JavaFX getStylsheets 在 IntelliJIdea 社区版本中不起作用
JavaFX getStylsheets not working in IntelliJIdea community Version
我正在尝试将 css 与 javaFX 结合使用。为了获得 css-Stylesheet 的设计,我尝试了这样的事情:
public void start(Stage stage) throws IOException {
stage = window;
window.setTitle("cssTest");
button1 = new Button("click me");
button1.setOnAction(e -> System.out.println("Hello!"));
StackPane layout1 = new StackPane();
layout1.getChildren().add(button1);
scene1 = new Scene(layout1, 300, 250);
scene1.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
window.setScene(scene1);
window.show();
}
首先,我的 IDE(IntelliJ Idea Community 版本)在 getStylesheets() 部分根本没有工作,在它下面加了下划线并告诉我重命名引用。我只是想测试一下,并尝试将应用程序的背景设置为黑色。这是我的 css 代码(文件名为 application.css):
.Scene{
-fx-background-color: black;
}
IntelliJ 也不会对 css 代码进行颜色编码。我首先在我的开始和我的主要方法所在的包中创建了 css 文件,但这会引发错误。看这里:
"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=62160:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\javafx-controls-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\javafx-graphics-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-base\javafx-base-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\javafx-fxml-16.jar -p C:\Users\timla\.m2\repository\org\openjfx\javafx-base\javafx-base-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\javafx-fxml-16-win.jar;C:\Users\timla\.m2\repository\org\controlsfx\controlsfx.1.0\controlsfx-11.1.0.jar;C:\Users\timla\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core[=12=].4.0\bootstrapfx-core-0.4.0.jar;C:\Users\timla\IdeaProjects\fxDemo\target\classes;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\javafx-graphics-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\javafx-controls-16-win.jar;C:\Users\timla\.m2\repository\com\dlsc\formsfx\formsfx-core.3.2\formsfx-core-11.3.2.jar -m com.example.fxdemo/com.example.fxdemo.HelloApplication
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because the return value of "java.lang.Class.getResource(String)" is null
at com.example.fxdemo/com.example.fxdemo.HelloApplication.start(HelloApplication.java:32)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
... 1 more
Exception running application com.example.fxdemo.HelloApplication
Process finished with exit code 1
然后我将其重构为 view.xml 旁边的资源。现在应用程序启动,但样式表未应用于它。奇怪的是,昨天 IDE 在 getStyleSheets() 上加了下划线,今天它接受了它,但它仍然无法正常工作。
正确找到 CSS 资源
总结原文中的问题post:
不要创建新阶段,使用传递给启动方法的阶段。
样式表是场景或节点的属性,不是阶段。因此,从适当的类型中获取样式表。
“Rename reference”只是一个重命名的重构选项,与你的问题无关
一个问题是找不到资源。要修复它,请参阅(可能重复):
附加点的答案
这些是在评论和问题编辑中提出的。
IntelliJ do also not colorcode the css code
Idea 确实这样做(对于 HTML 和 JavaFX CSS 属性名称),但它可能只是付费(终极版)的一个功能。参见:
strangely yesterday the IDE underlined getStyleSheets() and today it accepted it
那是因为在您的原始代码截图中,您试图在舞台上而不是场景上调用此方法。在您的编辑中,您已修复此问题并在场景上调用该方法。
另外,在编辑中您使用了正确的名称 getStylesheets()
,而不是 getStyleSheets()
。
I then refactored it to the resources next to view.xml
您没有 view.xml
并且 .xml
不是 .fxml
文件的正确类型,即使您尝试使用一个。
使用正确的CSSselect或
一旦解决了上述问题,就可以找到 CSS 样式表,但它仍然需要有效并使用正确的 selector 才能将样式应用于节点.
.Scene
没有 select 任何东西,没有样式 class 叫做 Scene
.
也许你的意思是.root
(将为场景全局设置属性)。
或者您可能想在代码中的容器上设置一个 ID,并仅为该容器设置背景。例如,在 Java 中写:
layout1.setId("layout");
并在 CSS 中写入:
#layout { /** CSS styles **/ }
提问者在评论中备注:
the .root fixed the problem
我正在尝试将 css 与 javaFX 结合使用。为了获得 css-Stylesheet 的设计,我尝试了这样的事情:
public void start(Stage stage) throws IOException {
stage = window;
window.setTitle("cssTest");
button1 = new Button("click me");
button1.setOnAction(e -> System.out.println("Hello!"));
StackPane layout1 = new StackPane();
layout1.getChildren().add(button1);
scene1 = new Scene(layout1, 300, 250);
scene1.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
window.setScene(scene1);
window.show();
}
首先,我的 IDE(IntelliJ Idea Community 版本)在 getStylesheets() 部分根本没有工作,在它下面加了下划线并告诉我重命名引用。我只是想测试一下,并尝试将应用程序的背景设置为黑色。这是我的 css 代码(文件名为 application.css):
.Scene{
-fx-background-color: black;
}
IntelliJ 也不会对 css 代码进行颜色编码。我首先在我的开始和我的主要方法所在的包中创建了 css 文件,但这会引发错误。看这里:
"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=62160:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\javafx-controls-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\javafx-graphics-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-base\javafx-base-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\javafx-fxml-16.jar -p C:\Users\timla\.m2\repository\org\openjfx\javafx-base\javafx-base-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\javafx-fxml-16-win.jar;C:\Users\timla\.m2\repository\org\controlsfx\controlsfx.1.0\controlsfx-11.1.0.jar;C:\Users\timla\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core[=12=].4.0\bootstrapfx-core-0.4.0.jar;C:\Users\timla\IdeaProjects\fxDemo\target\classes;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\javafx-graphics-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\javafx-controls-16-win.jar;C:\Users\timla\.m2\repository\com\dlsc\formsfx\formsfx-core.3.2\formsfx-core-11.3.2.jar -m com.example.fxdemo/com.example.fxdemo.HelloApplication
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because the return value of "java.lang.Class.getResource(String)" is null
at com.example.fxdemo/com.example.fxdemo.HelloApplication.start(HelloApplication.java:32)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
... 1 more
Exception running application com.example.fxdemo.HelloApplication
Process finished with exit code 1
然后我将其重构为 view.xml 旁边的资源。现在应用程序启动,但样式表未应用于它。奇怪的是,昨天 IDE 在 getStyleSheets() 上加了下划线,今天它接受了它,但它仍然无法正常工作。
正确找到 CSS 资源
总结原文中的问题post:
不要创建新阶段,使用传递给启动方法的阶段。
样式表是场景或节点的属性,不是阶段。因此,从适当的类型中获取样式表。
“Rename reference”只是一个重命名的重构选项,与你的问题无关
一个问题是找不到资源。要修复它,请参阅(可能重复):
附加点的答案
这些是在评论和问题编辑中提出的。
IntelliJ do also not colorcode the css code
Idea 确实这样做(对于 HTML 和 JavaFX CSS 属性名称),但它可能只是付费(终极版)的一个功能。参见:
strangely yesterday the IDE underlined getStyleSheets() and today it accepted it
那是因为在您的原始代码截图中,您试图在舞台上而不是场景上调用此方法。在您的编辑中,您已修复此问题并在场景上调用该方法。
另外,在编辑中您使用了正确的名称 getStylesheets()
,而不是 getStyleSheets()
。
I then refactored it to the resources next to view.xml
您没有 view.xml
并且 .xml
不是 .fxml
文件的正确类型,即使您尝试使用一个。
使用正确的CSSselect或
一旦解决了上述问题,就可以找到 CSS 样式表,但它仍然需要有效并使用正确的 selector 才能将样式应用于节点.
.Scene
没有 select 任何东西,没有样式 class 叫做 Scene
.
也许你的意思是.root
(将为场景全局设置属性)。
或者您可能想在代码中的容器上设置一个 ID,并仅为该容器设置背景。例如,在 Java 中写:
layout1.setId("layout");
并在 CSS 中写入:
#layout { /** CSS styles **/ }
提问者在评论中备注:
the .root fixed the problem