Java 使用GUI在数组中交替使用奇偶元素
我正在尝试使用更多的GUI内容,但我遇到了一些问题。我有一个jlabel数组。它们每个都包含一个从0到7的数字。我将背景颜色从黑色改为绿色,使数字“亮起来”。有没有办法让所有的偶数“亮起”,而让所有的奇数“暗起”,反之亦然?我试着使用计时器,但我的算法没有正常工作。下面是配置计时器的方法的代码。谢谢Java 使用GUI在数组中交替使用奇偶元素,java,arrays,swing,user-interface,jpanel,Java,Arrays,Swing,User Interface,Jpanel,我正在尝试使用更多的GUI内容,但我遇到了一些问题。我有一个jlabel数组。它们每个都包含一个从0到7的数字。我将背景颜色从黑色改为绿色,使数字“亮起来”。有没有办法让所有的偶数“亮起”,而让所有的奇数“暗起”,反之亦然?我试着使用计时器,但我的算法没有正常工作。下面是配置计时器的方法的代码。谢谢 public void configureAlternatingTimer() { if (this.timer != null) { this.timer.stop
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();
}
public void configurationalternatingtimer(){
如果(this.timer!=null){
this.timer.stop();
}
this.timer=新计时器(100,新ActionListener(){
已执行的公共无效操作(操作事件evt){
对于(int i=0;i删除for循环
,它阻止事件分派线程处理重新绘制
请求
相反,每次调用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;
}
}
});
Timer Timer=新定时器(500,新ActionListener(){
私有整数序列=0;
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(序列+“;”+(序列%2));
对于(int index=0;index7){
序列=0;
}
}
});
若要确定是奇数还是偶数,应考虑使用模数运算符%来确定数字是奇数还是偶数。模数将返回数字除以后的剩余部分
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
}
好的。我应该在哪里初始化我的I,我应该在每个if语句之后增加它吗?检查更新,我称它为sequence
。我已经选择在if
语句之后增加,但是没有理由你不能提前增加,你只会丢失0
好的,到目前为止我很喜欢。我应该改变我的灯光吗[I]调用两个灯光[顺序],因为一旦它达到8,我如何重置它以继续?请参阅最后的if
语句…您可以将序列停在那里,或者根据您的需要停止计时器…好的,我将GUI设置为进程应运行,直到单击另一个按钮。我想我想重置它。我不知道如何重置我尝试了:“sequence==8”“sequence=0”,但它仍然通过了else条件,因此抛出了一个越界错误。