首页 > 解决方案 > 在 javafx 中正确绑定对象并使其更新?

问题描述

我是 javaFX 和 FXML 的新手,一个星期以来,我试图将一组圆圈(它们是一些 GUI 指示器)绑定到某个方法,但没有得到它。

我没有完全理解:

  1. 我应该在哪里初始化我的圈子?
  2. 如何在第一个 Main.stage.show 之后更新我的 GUI?
  3. 我应该为此使用动画吗?

更具体地说,我的意图是看一些动画,这意味着看到 GUI 处于其初始状态,然后在延迟几秒钟后,我希望看到第一个圆圈被涂成红色。谢谢!

我当前的输出是我的 GUI 立即更新,并且我还收到以下错误:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.UIController.lambda$changeCircleColor$0(UIController.java:87)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.UIController.lambda$changeCircleColor$1(UIController.java:93)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
יונ 09, 2019 9:06:39 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX runtime of version 8.0.191
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;


public class Main extends Application {
    public static Stage stage = new Stage();
    public static Parent root = new Parent() {
    };

    /**
     * the main method of the program
     */
    public static void main(String args[]) {

        UIController uiController = new UIController();
        uiController.changeCircleColor();

        Application.launch(Main.class, args);
    }

    public void start(Stage stage){
        this.stage = stage;
        FXMLLoader loader = new FXMLLoader();
        try {
            loader.setLocation(getClass().getResource("resources/UserInterface.fxml"));
            this.root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        stage.setTitle("Troubleshooting project");
        stage.setScene(new Scene(root, 900, 700));
        stage.show();
    }

}

标签: javafxbinding

解决方案


推荐阅读