我對Java很新,我剛開始編寫這個愚蠢的小程序作爲GUI測試。它應該做的是有12個按鈕,將它們全部設置爲白色,連續將3個隨機按鈕設置爲黑色,將所有按鈕再次設置爲白色,等待一秒鐘然後重複。問題是,我似乎無法重複它。每當我嘗試在隨機按鈕變黑的代碼段放一段時間或循環時,它都不會運行。它不會給出任何錯誤,並且該進程本身也會運行,但不會出現窗口。這是類的代碼(減去進口語句):GUI中的循環問題(java)
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);
}
}
}
我在做什麼錯在這裏?
把一些斷點和監控你的行爲節目。它是否嘗試呼叫,正在呼叫您的所有setbackground呼叫,也許它只是不刷新顯示。我會首先確認邏輯。 – mwjohnson
試着看看[這個例子](http://stackoverflow.com/questions/18266087/change-jlabel-background-on-mouserelease-event/18293834#18293834) – MadProgrammer
你永遠不應該調用'update'或'paint '這個'panel.update(panel.getGraphics());'是非常糟糕的 – MadProgrammer