我一直在开发需要使用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);
}
这是我的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>
我得到的错误是:
WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "/F:/eclipse_progs/examplefx/bin/application/sample.fxml" not found.
但是指定的错误中的位置包含了它所引用的文件。任何人都可以解释错误的原因是什么。是代码还是插件问题?
在JavaFX中,如果要加载fxml文件,请使用FXMLLoader作为,
FXMLLoader.load(getClass().getResource("application/sample.fxml"));
要加载样式表,请使用as。
scene.getStlyeShees().add(getClass().getResource("application/sample.css").toExternalForm);
在您的代码中,您将fxml文件添加到场景的样式表列表中,这是错误的。尝试如上所述使用FXMLLoader加载您的fxml文件。
参考文档:http : //docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句