我对java很陌生,我刚开始编写这个愚蠢的小程序作为GUI测试。它应该做的是有12个按钮,将它们全部设置为白色,连续将3个随机按钮变为黑色,再次将所有按钮设置回白色,等待一秒钟然后重复。问题是,我似乎无法让它重复。每次我尝试在使随机按钮变黑的代码部分周围放置一段时间或 for 循环,它根本不会运行。它没有给出任何错误,并且进程本身确实运行,但没有出现任何窗口。这是该类的代码(减去 import 语句):
public class testingness extends JFrame {
JButton one, two, three, four, five, six, seven, eight, nine, ten, eleven,
twelve;
JPanel panel;
testingness(String title) {
super(title);
this.init();
this.setSize(800, 800);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
void init() {
panel = new JPanel();
panel.setLayout(new GridLayout(3, 4));
one = new JButton();
one.setBackground(Color.white);
two = new JButton();
two.setBackground(Color.white);
three = new JButton();
three.setBackground(Color.white);
four = new JButton();
four.setBackground(Color.white);
five = new JButton();
five.setBackground(Color.white);
six = new JButton();
six.setBackground(Color.white);
seven = new JButton();
seven.setBackground(Color.white);
eight = new JButton();
eight.setBackground(Color.white);
nine = new JButton();
nine.setBackground(Color.white);
ten = new JButton();
ten.setBackground(Color.white);
eleven = new JButton();
eleven.setBackground(Color.white);
twelve = new JButton();
twelve.setBackground(Color.white);
panel.add(one);
panel.add(two);
panel.add(three);
panel.add(four);
panel.add(five);
panel.add(six);
panel.add(seven);
panel.add(eight);
panel.add(nine);
panel.add(ten);
panel.add(eleven);
panel.add(twelve);
this.add(panel);
while (true) {
randomness();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void randomness() {
for (int timesdone = 0; timesdone < 4; timesdone++) {
panel.update(panel.getGraphics());
Random r = new Random();
int rand = r.nextInt(12);
if (rand == 0) {
one.setBackground(Color.black);
} else if (rand == 1) {
two.setBackground(Color.black);
} else if (rand == 2) {
three.setBackground(Color.black);
} else if (rand == 3) {
four.setBackground(Color.black);
} else if (rand == 4) {
five.setBackground(Color.black);
} else if (rand == 5) {
six.setBackground(Color.black);
} else if (rand == 6) {
seven.setBackground(Color.black);
} else if (rand == 7) {
eight.setBackground(Color.black);
} else if (rand == 8) {
nine.setBackground(Color.black);
} else if (rand == 9) {
ten.setBackground(Color.black);
} else if (rand == 10) {
eleven.setBackground(Color.black);
} else if (rand == 11) {
twelve.setBackground(Color.black);
}
one.setBackground(Color.white);
two.setBackground(Color.white);
three.setBackground(Color.white);
four.setBackground(Color.white);
five.setBackground(Color.white);
six.setBackground(Color.white);
seven.setBackground(Color.white);
eight.setBackground(Color.white);
nine.setBackground(Color.white);
ten.setBackground(Color.white);
eleven.setBackground(Color.white);
twelve.setBackground(Color.white);
}
}
}
我在这里做错了什么?