【问题标题】:Button Action in JavaFX executes in a wrong sequenceJavaFX 中的按钮操作以错误的顺序执行
【发布时间】:2014-06-30 08:47:23
【问题描述】:

更新 1:这是一个更有趣的事情。当我删除最后一行 { mainapp.showReportingScreen() ;} 现在程序首先执行 findAplliedJars 方法,然后显示处理屏幕。


我在 JavaFX 中工作,并为按钮操作而苦苦挣扎。我想要的是,当那个按钮被点击时:

  1. 做点什么;
  2. 显示另一个屏幕;
  3. 做另一件事;
  4. 显示另一个屏幕;

但是,我的程序跳过了 2. 点。

这是我的代码:

public class SomeController implements Initializable {
    .
    .
    .
@Override
public void initialize(URL location, ResourceBundle resources) {
           .
           .
           .
    someButton.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent event) {             

            Report.setUserName(userNameField.getText());
            Report.setPassword(passwordField.getText());
            Report.setSmAdress(smAdressField.getText());
            mainApp.showProcessingScreen();
            Report.findAppliedJars();
            mainApp.showReportingScreen();

        }
    });        
}   
    . 
    .
    .
}

因此,在不显示 processingScreen 的情况下,我的程序执行 findAppliedJars(),然后显示 reportingScreen

可能是什么问题?谢谢。


更新 2:这是我的 showProcessingScreen() 方法:

public void showProcessingScreen() {
        try {
            // Load Report Screen
            FXMLLoader loader = new FXMLLoader();               
            loader.setLocation(MainApp.class.getResource("ProcessingScreen.fxml"));         
            AnchorPane processingScreen = (AnchorPane) loader.load();           
            // Set report screen into the center of root layout.
            rootLayout.setCenter(processingScreen);
            ProcessingScreenController controller = loader.getController();
            controller.setMainApp(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • 你能把processingScreen的代码贴出来吗?
  • 显示处理屏幕时可能不会停止执行
  • 我一会儿把代码贴出来。

标签: java button controller javafx initialization


【解决方案1】:

除非您以某种方式暂停执行,否则程序将继续执行,并且基本上会跳过处理屏幕的显示。要在一定时间内显示您的处理屏幕,您需要使用animation 并在动画完成后显示您的报告屏幕。请注意,下面的代码只是您的最终结果的概要,无法编译。

Transition transition;
// Set up your transition or animation
transition = buildTransition();
// Show your processing screen within the transition
showProcessingScreen();
transition.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent arg0) {
        Report.findAppliedJars();
        mainApp.showReportingScreen();
    }       
});

如果您在显示处理屏幕时尝试在后台加载某些内容,则必须使用concurrency。如果你走这条路,代码应该与上面类似,除了你将使用Task classsetOnSucceeded method。还有其他方法可以执行此操作,但我相信这是最简单的方法。

【讨论】:

  • 其实 findAplliedJars 方法需要一些时间。所以我认为当该方法运行时,应该看到处理屏幕。现在我等待 findAplliedJars 方法完成,然后是报告屏幕。
  • 是的,在这种情况下,您需要使用 Task 类。
猜你喜欢
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
相关资源
最近更新 更多