有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用GUI在数组中交替使用奇偶元素

我正在尝试使用更多的GUI功能,但我遇到了一些问题。我有一系列的jlabel。每一个都包含一个从0到7的数字。我将背景颜色从黑色改为绿色,使数字“亮起来”。有没有办法让所有偶数“亮”起来,同时让所有奇数保持黑暗,反之亦然?我试着使用计时器,但我的算法没有正常工作。下面是配置计时器的方法的代码。谢谢

    public void configureAlternatingTimer() {
    if (this.timer != null) {
        this.timer.stop();
    }

    this.timer = new Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            for (int i = 0; i <= 8; i++) {
                if (i == 0 || i == 2 || i == 4 || i == 6) {
                    lights[1].setBackground(Color.black);
                    lights[3].setBackground(Color.black);
                    lights[5].setBackground(Color.black);
                    lights[7].setBackground(Color.black);
                    lights[i].setBackground(Color.green);
                }
                if (i == 1 || i == 3 || i == 5 || i == 7) {
                    lights[0].setBackground(Color.black);
                    lights[2].setBackground(Color.black);
                    lights[4].setBackground(Color.black);
                    lights[6].setBackground(Color.black);
                    lights[i].setBackground(Color.green);
                }
                if(i==8) {
                    return;
                }
            }

        }

    });
    this.timer.start();

}

此外,我正在尝试模拟一个“拉尔森扫描仪”,它会亮到7,然后再回到0,然后重复。我可以让它从0变为7,这只是我遇到麻烦的部分。谢谢


共 (2) 个答案

  1. # 1 楼答案

    要确定一个数是奇数还是偶数,应该考虑使用模运算符%来确定一个数是奇数还是偶数。模数将返回一个数被除后的余数

    For example:
        4 / 2 = 2r0
        5 / 2 = 2r1
        6 / 2 = 3r0
        7 / 2 = 3r1
        so on and so forth...
    
    if(i % 2 == 0) {
        // even
    } else {
        // odd
    }
    
  2. # 2 楼答案

    删除for-loop,这会阻止事件调度线程处理repaint请求

    相反,每次调用actionPerformed方法时,更新某种计数器,然后对其执行操作,例如

    this.timer = new Timer(100, new ActionListener() {
        private int sequence = 0;
        public void actionPerformed(ActionEvent evt) {
            if (sequence % 2 == 0) {
                lights[1].setBackground(Color.black);
                lights[3].setBackground(Color.black);
                lights[5].setBackground(Color.black);
                lights[7].setBackground(Color.black);
                lights[sequence].setBackground(Color.green);
            } else {
                lights[0].setBackground(Color.black);
                lights[2].setBackground(Color.black);
                lights[4].setBackground(Color.black);
                lights[6].setBackground(Color.black);
                lights[sequence].setBackground(Color.green);
            }
            
            sequence++;
            if (sequence > 7) {
                // This seems to be important...?
            }
        }
    });
    

    根据评论更新

    这应该显示所有的可能性或所有的平衡

    Timer timer = new Timer(500, new ActionListener() {
        private int sequence = 0;
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(sequence + "; " + (sequence % 2));
    
            for (int index = 0; index < lights.length; index++) {
    
                if (index % 2 == 0 && sequence % 2 == 0 || index % 2 != 0 && sequence % 2 != 0) {
                    lights[index].setBackground(Color.GREEN);
                } else {
                    lights[index].setBackground(Color.BLACK);
                }
    
            }
    
            sequence++;
            if (sequence > 7) {
                sequence = 0;
            }
        }
    });