JavaFX-事件期间的操作

JavaFX-事件期间的操作,java,events,javafx,javafx-2,scene,Java,Events,Javafx,Javafx 2,Scene,我试图在javaFX中的事件期间影响UI元素 void buttonClicked(ActionEvent e) { labelInfo.setText("restarting - might take a few seconds"); jBoss.restart(); labelInfo.setText("JBoss successfully restarted"); } 操作“jBoss.restart()”将等待jBoss重新启动 问题是: 文本“重新启动-…”不

我试图在javaFX中的事件期间影响UI元素

void buttonClicked(ActionEvent e) {
    labelInfo.setText("restarting - might take a few seconds");
    jBoss.restart();
    labelInfo.setText("JBoss successfully restarted");
}
操作“jBoss.restart()”将等待jBoss重新启动

问题是:

文本“重新启动-…”不显示。应用程序等待JBoss重启,然后显示文本“JBoss成功重启”

我的想法: 事件完成后,将刷新场景。因此,不会发生第一次标签更改


如何在事件期间显示信息消息?

问题在于FX线程没有安全操作。所以我猜
jBoss.restart()
要花很多时间。所以你必须把这个命令放到服务中。此外,我还向您推荐一个进度指示器,向用户显示您正在进行长时间的操作

这是一个例子,但我鼓励你们去深入研究一下。也许还有其他事情可以帮助你

import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Label labelInfo;
    private Button button;
    private ProgressIndicator progressIndicator;

    @Override
    public void start(Stage stage) throws Exception {
        VBox vbox = new VBox(5);
        vbox.setAlignment(Pos.CENTER);
        labelInfo = new Label();
        button = new Button("Restart");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                buttonClicked(event);
            }
        });
        progressIndicator = new ProgressIndicator(-1);
        progressIndicator.setVisible(false);
        vbox.getChildren().addAll(labelInfo, progressIndicator, button);

        Scene scene = new Scene(vbox, 300, 200);
        stage.setScene(scene);
        stage.show();
    }

    void buttonClicked(ActionEvent e) {
        Service<Void> service = new Service<Void>() {
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {
                        updateMessage("restarting - might take a few seconds");
                        // Here the blocking operation
                        // jBoss.restart();
                        Thread.sleep(10000);
                        updateMessage("JBoss successfully restarted");
                        return null;
                    }
                };
            }
        };
        // Make the progress indicator visible while running
        progressIndicator.visibleProperty().bind(service.runningProperty());
        // Bind the message of the service to text of the label
        labelInfo.textProperty().bind(service.messageProperty());
        // Disable the button, to prevent more clicks during the execution of
        // the service
        button.disableProperty().bind(service.runningProperty());
        service.start();
    }
}
导入javafx.application.application;
导入javafx.concurrent.Service;
导入javafx.concurrent.Task;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ProgressIndicator;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类测试扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
自有品牌labelInfo;
私人按钮;
私人进展指标;
@凌驾
public void start(Stage)引发异常{
VBox VBox=新的VBox(5);
vbox.setAlignment(位置中心);
labelInfo=新标签();
按钮=新按钮(“重新启动”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
按钮点击(事件);
}
});
progressIndicator=新的progressIndicator(-1);
progressIndicator.setVisible(假);
vbox.getChildren().addAll(labelInfo、progressIndicator、按钮);
场景=新场景(vbox,300200);
舞台场景;
stage.show();
}
作废按钮点击(ActionEvent e){
服务=新服务(){
@凌驾
受保护的任务createTask(){
返回新任务(){
@凌驾
受保护的Void调用()引发异常{
updateMessage(“重新启动-可能需要几秒钟”);
//这里是阻塞操作
//jBoss.restart();
睡眠(10000);
updateMessage(“JBoss成功重启”);
返回null;
}
};
}
};
//使进度指示器在运行时可见
progressIndicator.visibleProperty().bind(service.runningProperty());
//将服务的消息绑定到标签的文本
labelInfo.textProperty().bind(service.messageProperty());
//禁用该按钮,以防止在执行过程中再次单击
//服务
button.disableProperty().bind(service.runningProperty());
service.start();
}
}

当然可以。我们必须记住,JavaFX应用程序线程仅用于更新和操作图形用户界面,不应用于处理某些内容。我完全同意你的回答