我有一个变量的问题,我用来跟踪用户活动的状态。在GUI中,我有一个按钮,单击该按钮会启动第二个GUI。在该GUI中,用户可以完成在第一个GUI中启动的活动,也可以不完成。
如果用户取消了第二个GUI,那么将返回到第一个GUI,保留所有变量和列表的当前值。如果第二个GUI完成了第一个GUI的活动,那么应该重置所有变量和列表。
为了跟踪这一点,我有一个变量(布尔完整)初始设置为FALSE。在第二个GUI中,当单击"OK"按钮(而不是单击"Cancel"按钮)时,第二个GUI调用第一个GUI中的方法,将"complete"的值更改为TRUE。
为了查看到底发生了什么,我在几个点上设置了System.out.println,这样我就可以看到"complete"的值。我看到的是:
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的方法开始时,"complete"的值= 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成功地改变了"完成"的值,但当我返回到第一个GUI时,它仍然认为完成为FALSE?我怎么才能避开这个问题呢?
尝试让第二个GUI的控制器调用第一个GUI控制器中的方法来修改完整的变量
例如:
// Code to handle the OK button being pressed
@Override
public void handle(ActionEvent t) {
// Do validation and work
//reference to the first controller object
firstController.setComplete(true);
}