我對Java很陌生,但遇到了一個我不明白的問題。我想要一個jbutton顯示一個窗口並隱藏另一個窗口。我用無限循環做了這個,當jbutton被按下時,監聽變量的變化。無法識別變量變化
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) { //Jbutton in question
NewJFrame frame = new NewJFrame(); //another JFrame from which I created the public variable(Visible)
frame.Visible = false;
}
while (always == true) { **//code in main method to test variable change**
if (frame1.Visible == true) {
frame1.show();
frame2.hide();
}
else {
frame1.show();
frame2.hide();
}
Interrestingly,這作品時,我做同樣的與被創建的公共變量(可見光)JFrame中,AKA內的按鈕:[IF]的一部分,但其他人不執行。
有什麼我可以做的,讓主方法rocognize這個變量的變化?
爲什麼不只是讓按鈕點擊事件切換每個幀的可見性?有一個'while(true)'循環是不必要的,而且我同意CPU – climbage
,你應該只聽點擊事件。循環沒有理由,當有必要使用循環時,不要忘記打破循環。 –