JavaFX按钮句柄()不调用方法

JavaFX button handle() not calling methods

这似乎是一个很基本的问题,答案就在我面前,但我仍然不知道哪里出了问题。我有一个按钮,在处理单击事件时,我更改了标签的样式和文本。之后,我调用了一个方法,完成后再次更改样式。

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

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

我尝试在 setStyle() 和 connect() 之间让线程休眠一秒钟(以防我的速度太慢),但无济于事。我将不胜感激任何帮助,并希望在此过程中学到一些东西。

这是我的代码:

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();
        }
    });

并在您的连接方法中用 Platform.runLater

包围您的 ui 更新方法
Platform.runLater(() -> {
    loginStatus.setStyle("-fx-text-fill:#009933");
    loginStatus.setText("Online");
}) ;