JavaFX静态与非静态 [英] JavaFX Static vs non Static
问题描述
美好的一天,
为什么JavaFX有问题,如果我在一个方法之外创建一个静态标签,但是对于像a这样的形状不会有同样的问题球体或矩形。
Why does JavaFX have a problem, if i create a static Label outside a method, but doesen't have the same problem with shapes like a sphere or a rectangle.
以下工作正常:
static Rectangle upperBorder = new Rectangle(0, 0, 10, 10);
但这会产生异常:
static Label myScore = new Label("Test");
我必须按以下方式创建标签:
I have to create the Label the following way:
Label myScore = new Label("Test");
这是没有static关键字。
This is without the "static" keyword.
这是错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at pong.Main.<clinit>(Main.java:24)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
... 4 more
推荐答案
错误很可能不是由 static
关键字引起的。
The error is most likely not caused by the static
keyword.
考虑这个简单的测试程序:
Consider this simple test program:
public class Test {
static Rectangle a = new Rectangle(0, 0, 10, 10);
static Label b = new Label("b");
public static void main(String[] args) {
}
}
启动时抛出异常:
Exception in thread "main" java.lang.ExceptionInInitializerError
at Test.<clinit>(Test.java:7)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
... 1 more
这暗示JavaFX应用程序平台未启动。在此示例中删除 static
关键字时,错误似乎消失,但这是因为标签 b
不是在代码中使用。
This hints at the JavaFX Application Platform not being started. The error seems to disappear when the static
keyword is removed in this example, but that is because the label b
is not used in the code.
启动与JavaFX应用程序相同的类可确保在第一个控件之前初始化Platform
已创建:
Launching the same class as a JavaFX Application ensures that the Platform is initialized before the first Control
is created:
public class Test extends Application {
static Rectangle a = new Rectangle(0, 0, 10, 10);
static Label b = new Label("b");
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
}
}
Rectangle
和<$之间的区别您的代码中的c $ c>标签是标签
是 Control
和矩形
不是。没有正在运行的JavaFX平台,无法实例化标签
。
The difference between Rectangle
and Label
in your code is that Label
is a Control
and Rectangle
is not. Label
can not be instantiated without a running JavaFX Platform.
这篇关于JavaFX静态与非静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!