我遇到了一個用於跟蹤用戶活動狀態的變量的問題。在GUI中,我有一個按鈕,點擊按鈕後會啓動第二個GUI。在該GUI中,用戶可以完成在第一個GUI中啓動的活動或不完成。showandwait更改變量值
如果用戶取消第二個GUI,那麼這個想法是返回到第一個GUI,將所有變量和列表保留其當前值。如果第二個GUI完成第一個GUI的活動,則應重置所有變量和列表。
要跟蹤這個,我有一個變量(布爾完成)最初設置爲FALSE。在第二個GUI中,當單擊「OK」按鈕(而不是「Cancel」按鈕)時,第二個GUI會在第一個GUI中調用一個方法,將「complete」的值更改爲TRUE。
爲了看看到底發生了什麼,我在幾個點上有了System.out.println,讓我能夠看到「完整」的價值。我看到的是這樣的:
Launching first GUI - complete = FALSE
Launching second GUI - complete = FALSE
Clicking "OK" in second GUI - complete = TRUE
Second GUI closes itself, returning to complete first GUI activity
First GUI finishes activity with complete = FALSE
我假設它是因爲我發動了showandwait第二GUI,當含有showandwait的方法開始的「完整」的值= FALSE。該值在show和wait的WAIT部分中發生變化,然後該方法繼續進行,並且這是我得到的值仍然爲FALSE的值,儘管它已更改爲TRUE。
這裏是有問題的代碼的摘要(如果你需要確切的代碼,它是更長的時間,但我可以張貼在要求):
completeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
try {
System.out.println("b4 calc = " + complete); // complete = FALSE
// all the code to create the calcStage
calcStage.showAndWait(); // second GUI, which calls a method in THIS
// class that changes complete to TRUE. That method
// (in THIS file) also has a println that shows the change.
getComplete(); // tried adding this method to check the value of
// "complete" after the change made by the calcStage
// (which calls a method in this same file)
System.out.println("Complete? " + complete);
// this shows complete = FALSE,
// though in the calcStage it was changed to TRUE
if (salecomplete) {
// code that should reset all variables and lists if the activity was completed
}
}
}
}
這裏的問題是爲什麼第二GUI成功改變「complete」的值,但是當我返回到第一個GUI時,它仍然會看到完整的爲FALSE?我該如何解決這個問題?
謝謝,但我已經試過了。由於某種原因,雖然從第二個GUI調用的方法(在第一個GUI中)將完成成功更改的值報告爲TRUE,但當第二個GUI關閉並且第一個GUI恢復調用第二個GUI的方法時,值爲再次報告爲FALSE。這是我不明白的。 – John
當你調用該方法時,你是否真的在第一個控制器中設置了實例變量? – Yarrgh