在 JavaFX 项目中加载 fxml 文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22532267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error loading fxml file in a JavaFX project
提问by Coder 477
I have been working on a desktop application, where I need to use JavaFX. I have created an fxml
file in a JavaFX project on eclipse. I build the scene in scene builder.when I am trying to run the main Java file, the changes which are made using scene builder on the fxml
file are not getting reflected on the main window.and the screen is displayed blank. here is my code.
我一直在开发桌面应用程序,我需要在其中使用 JavaFX。我fxml
在 Eclipse 上的 JavaFX 项目中创建了一个文件。我在场景构建器中构建场景。当我尝试运行主 Java 文件时,使用场景构建器对fxml
文件所做的更改没有反映在主窗口上。屏幕显示为空白。这是我的代码。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root,400,400,Color.BLACK);
scene.getStylesheets().add(getClass().getResource("/application/sample.fxml").getPath());
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
And this is my sample.fxml
file code:
这是我的sample.fxml
文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?scenebuilder-background-color 0x00ffa3ff?>
<AnchorPane prefHeight="379.0" prefWidth="549.0001220703125" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
<children>
<Button layoutX="219.0" layoutY="156.0" mnemonicParsing="false" text="hello" />
</children>
</AnchorPane>
The error I got was:
我得到的错误是:
WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "/F:/eclipse_progs/examplefx/bin/application/sample.fxml" not found.
But the location in the error specified contains the file it is referring to. Can anyone explain what might be the reason with the error. Is it with the code or is with the issues of plugin?
但是指定的错误中的位置包含它所指的文件。任何人都可以解释错误的原因。是代码问题还是插件问题?
采纳答案by AJJ
In JavaFX, if you want to load the a fxml file, then use the FXMLLoader as,
在 JavaFX 中,如果你想加载一个 fxml 文件,那么使用 FXMLLoader 作为,
FXMLLoader.load(getClass().getResource("application/sample.fxml"));
To load stylesheet use as.
要加载样式表,请使用 as。
scene.getStlyeShees().add(getClass().getResource("application/sample.css").toExternalForm);
In your code, you are adding the fxml file in the stylesheets list of the scene which is wrong. Try using FXMLLoader as said above to load your fxml file.
在您的代码中,您在场景的样式表列表中添加了 fxml 文件,这是错误的。尝试使用 FXMLLoader 如上所述加载您的 fxml 文件。
Refer docs: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
参考文档:http: //docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
回答by Patrick
Try this start method and place your directory application/<NAME>.fxml
in the same directory where your Main.class
is
fxml files getting loaded with the static FXMLLoader.load()
method
with getStylesheets.add(...)
you can add .css files to your scene.
试试这个启动方法并将你的目录application/<NAME>.fxml
放在你Main.class
的 fxml 文件被静态FXMLLoader.load()
方法加载的同一目录中,getStylesheets.add(...)
你可以将 .css 文件添加到你的场景中。
@Override
public void start(Stage primaryStage) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource("application/demo.fxml"));
Scene scene = new Scene(parent);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}