styles - 在运行时找不到场景生成器中的连接样式

标签 styles scenebuilder gluon

我在 Scene Builder for Java 8 中创建 Pane。我的 .css 文件存储在 /rescouces/css/app.css 中。我在 Scene Builder 中连接样式表,一切正常。但在我启动我的应用程序后,我收到异常错误: 原因:javafx.fxml.LoadException:无效资源:在类路径上找不到/../style/app.css

如何解决这个问题?我每次都需要将路径重命名为 .fxml 中的 css

enter image description here

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="912.0" prefWidth="1368.0" styleClass="app" stylesheets="@/style/app.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mypod.tablet.controller.MainController">
    <children>
        <AnchorPane fx:id="contentPane" layoutX="248.0" layoutY="138.0" stylesheets="@/style/content.css" AnchorPane.bottomAnchor="94.0" AnchorPane.leftAnchor="250.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="115.0">
            <styleClass>
                <String fx:value="block" />
                <String fx:value="content-block" />
            </styleClass>
        </AnchorPane>
    </children>
</AnchorPane>

加载fxml:

FXMLLoader loader = new FXMLLoader();

this.primaryStage.setScene(new Scene(loader.load(Util.getResource("/fxml/main.fxml"))));

最佳答案

问题

为了重现该问题,并根据该问题的评论,需要执行以下操作:

主类

@Override
public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader();
    Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

src/main/resources/fxml/scene.fxml 下的 FXML :

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Button fx:id="button" text="Click Me!" />
    </children>
</AnchorPane>

src/main/resources/styles/styles.css下的CSS

.button {
    -fx-font-size: 2em;
}

项目运行,但打印出以下错误:

null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.

手动编辑 FXML 文件并删除父点时:

stylesheets="@/styles/styles.css"

似乎解决了问题并且运行良好且没有警告,这会阻止场景生成器找到 css 文件,因此不应该这样做。

解决方案

  • 使用getResourceAsStream不建议检索 FXML 文件,只需使用 getResource() .

这有效:

FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));
  • 使用FXMLLoader空构造函数不是推荐的方法,而是使用 static load方法。

这有效:

Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));
  • 最后,不需要类加载器。类加载器是基于流的,它不知道您从中检索它的类,并尝试从包 "fxml/scene.fxml" 中查找。 。另一方面,Class.getResource()是基于 URL 的,它会查找与类相关的资源,因此您需要将路径设置为项目根目录 "/fxml/scene.fxml"

应该这样调用它:

Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));

或者如果您需要加载程序(例如检索 Controller ),这也是推荐的方式:

FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();

这个post值得一读。

关于styles - 在运行时找不到场景生成器中的连接样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53309349/

相关文章:

css - 需要 Bootstrap 的大集合,但需要更大的灵活性

css - 有没有 facebook css 样式的库?

java - 如何获取 jfxmobile 插件以使用 MagnetometerService 等

Gluon 移动跨平台广告查看

ios - 如何使用 Gluon 应用程序打开/发送 URL 到 iOS 上的另一个应用程序?

wpf - 边框背景上的 Style.DataTrigger

c# - 改变 parent 的风格

java - 如何计算javafx文本区域中的行数

java - 如何将模态对话附加到 JavaFX 中的主窗口?

java - 无法在 Netbeans JavaFX 项目中集成使用 SceneBuilder 生成的 FXML