JavaFX 按钮handle() 不调用方法

标签 java events javafx label

这似乎是一个非常基本的问题,答案就在我面前,但我仍然无法弄清楚出了什么问题。我有一个按钮,在处理单击事件时,我更改标签的样式和文本。之后,我调用一个方法,在完成后再次更改样式。

我的问题是,handle() 方法中的样式更改不会影响我的标签,而是直接从默认样式更改为 connect() 设置的样式。

请注意,这并不是因为它变化太快,而是因为 connect() 方法在连接到远程服务器时通常需要一整秒左右才能完成。

我尝试在 setStyle() 和 connect() 之间让线程 hibernate 一秒钟(以防我的速度太慢),但无济于事。我将非常感谢任何帮助,并希望一路上能学到一些东西。

这是我的代码:

Button loginButton = new Button();

    loginButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            loginStatus.setText("Loggin in...");

            //The line below should change the color until connect does it's thing, but it doesn't
            loginStatus.setStyle("-fx-text-fill:#ffcc00"); 

            connect(username.getText(), password.getText(), serverField.getText());
        }
    });

connect() 看起来像这样:

private void connect(String username, String password, String server) {
    try {
        api = new DiscordBuilder(username, password).build();
        api.login();
        api.joinInviteId(server);
        api.getEventManager().registerListener(new ChatListener(api));

        //Instead, it goes straight from the old style to the style set below.
        loginStatus.setStyle("-fx-text-fill:#009933");

        loginStatus.setText("Online");
    } catch (NoLoginDetailsException e) {
        loginStatus.setText("No login details!");
        loginStatus.setStyle("-fx-text-fill:#cc3300");
        e.printStackTrace();
    } catch (BadUsernamePasswordException e) {
        loginStatus.setText("Bad username or password!");
        loginStatus.setStyle("-fx-text-fill:#cc3300");
        e.printStackTrace();
    } catch (DiscordFailedToConnectException e) {
        loginStatus.setText("Failed to connect!");
        loginStatus.setStyle("-fx-text-fill:#cc3300");
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最佳答案

您需要的是 Task .

亦如所述here

Implementing long-running tasks on the JavaFX Application thread inevitably makes an application UI unresponsive. A best practice is to do these tasks on one or more background threads and let the JavaFX Application thread process user events.

所以你的代码应该是这样的

    Button loginButton = new Button();
    loginButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            loginStatus.setText("Loggin in...");
            //The line below should change the color until connect does it's thing, but it doesn't
            loginStatus.setStyle("-fx-text-fill:#ffcc00"); 
            Task<Void> task = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    connect(username.getText(), password.getText(), serverField.getText());
                    return null;
                }
            };
            new Thread(task).start();
        }
    });

并在你的 connect 方法中用 Platform.runLater 包围你的 ui 更新方法

Platform.runLater(() -> {
    loginStatus.setStyle("-fx-text-fill:#009933");
    loginStatus.setText("Online");
}) ;

关于JavaFX 按钮handle() 不调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383940/

相关文章:

java - 如何初始化继承的值,并使用抽象方法c++?

javascript - 单击、输入、更改复选框?

java - 如何在 ImageView 中使用触摸获取实际图像坐标?

JavaFX 2 TextArea 根据其内容改变高度

java - 即使显式提供了模块路径,Eclipse 也找不到模块

java - 在 Liferay 5.2 下运行 Coldfusion 9 以服务 portlet 时出错

java - 如何在android中进行点对点VoIP通话

.net - 如何获得事件的反馈?

java - JavaFX 中使用默认 modena 样式的按钮宽度

java - 如何通过使用父类(super class)来减少代码?