0
因此,當我運行此代碼嘗試更改背景GUI崩潰並陷入無限while循環忽略事件偵聽器。這裏是代碼:while循環忽略事件偵聽器
private Panel getPanel1() {
if (panel1 == null) {
panel1 = new Panel();
panel1.setLayout(new GridBagLayout());
while(frame.isVisible()){
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
frame.setVisible(false);
}
});
int r = (int) (Math.random()*255);
int g = (int) (Math.random()*255);
int b = (int) (Math.random()*255);
Color c = new Color(r, g, b);
panel1.setBackground(c);
try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
/*panel1.setVisible(false);
frame.setVisible(false);*/
System.exit(0);
}
});
}
}
return panel1;
}
,而不是退出終止程序或事件改變它只是顯示面板,所以沒有任何其他背景的循環,我必須迫使它退出。我該怎麼辦?