所以我有一個非常大的項目,但最終將問題歸結爲以下情況。爲什麼我的循環只能在使用SOUT時繼續?
我有主類 「LoopTest」 和另一個階級 「RandomClassObject」
LoopTest:
public class LoopTest {
static RandomClassObject rco;
public static void main(String[] args) {
rco = new RandomClassObject();
System.out.println("RCO has finished");
}
}
RandomClassObject:
public class RandomClassObject {
JFrame frame = new JFrame();
JButton button = new JButton("Click Me");
boolean created = false;
public RandomClassObject() {
button.addActionListener(this::buttonActionPerformed);
frame.add(button);
frame.setVisible(true);
while (!created) {
//System.out.println("Done"); //This needs to be uncommented to work.
}
System.out.println("It was been Created");
}
public void buttonActionPerformed(ActionEvent evt) {
created = true;
}
所以我想讓我的RandomClassObject等到按下按鈕。 我有一個布爾「創建」並有一個while循環循環,直到所述布爾值更改爲true。
當與SOUT「完成」運行註釋掉,我點擊按鈕,但從來沒有得到第二個SOUT「它已被創建」。
當SOUT「完成」取消註釋運行時,我收到垃圾郵件與「完成」的和一旦我點擊按鈕我收到SOUT「它已被創建」。
我需要幫助瞭解爲什麼我必須在我的循環中放入SOUT以便永久退出buttonClick。
對不起,如果這是一個明顯的錯誤,並感謝任何答覆!
謝謝@teppic!我的完整方法之前的synchronized關鍵字有所幫助。我知道你說使用按鈕的事件監聽器會更好,但我需要這個循環來暫停主程序繼續,直到對象RCO被點擊。 –
有幾種方法可以做到這一點。我編輯了答案並添加了一條建議。 – teppic