javafx - 在 javafx 中正确绑定对象并使其更新?
问题描述
我是 javaFX 和 FXML 的新手,一个星期以来,我试图将一组圆圈(它们是一些 GUI 指示器)绑定到某个方法,但没有得到它。
我没有完全理解:
- 我应该在哪里初始化我的圈子?
- 如何在第一个 Main.stage.show 之后更新我的 GUI?
- 我应该为此使用动画吗?
更具体地说,我的意图是看一些动画,这意味着看到 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();
}
}
解决方案
推荐阅读
- python - PUT 请求:JSONObject 文本必须在 1 [字符 2 第 1 行] 处以 '{' 开头
- android - 如何将自定义字体设置为在 Android 中加载了 WebView 的 html?
- php - 无法使用 ZipArchive 打开 zip
- java - Gradle 在 Ubuntu 终端中从版本 6 降级到版本 5
- c# - Kestrel 服务器因异常而崩溃
- m2doc - 无效类型文字导入 - 如何导入和使用我的自定义服务
- wordpress - WP/Woo 在发布帖子时向自定义元收件人发送电子邮件
- javascript - 最新的 npm cra 安装错误
- html - 如何让底部边框仅显示在填充文本下
- python - 显示限时消息 wxPython