css - 如何在 JavaFx Spring 应用程序中包含 css 文件

标签 css spring javafx fxml

当我在 JavaFx Application 类中包含 css 资源的路径时,元素运行完美。

public class SampleApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(SampleAppFactory.class);

        SampleController sampleController = context.getBean(SampleController.class);
        Scene scene = new Scene((Parent) sampleController.getView(), 320, 240);
        scene.getStylesheets().add("/resources/css/fxmlapp.css");

        stage.setScene(scene);
        stage.setTitle("JFX2.0 Sprung");
        stage.show();
    }
}

但是,当我在 FXML 文件中包含 css 路径时,编译失败。

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<StackPane fx:id="view" prefHeight="98.0" prefWidth="160.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.rev.SampleController">
  <children>
    <Button fx:id="printBtn" onAction="#print" text="Click Me" />
  </children>
  <stylesheets>
    <URL value="@../css/fxmlapp.css" />
  </stylesheets>
</StackPane>

注意 <URL value="@../css/fxmlapp.css" /> 行中的内容.

我想使用 FXML 中包含的 css,因为使用那里的 css 设计 FXML 更容易,您知道 - 您可以看到特定的 css 属性如何影响页面的外观。我怎样才能让它发挥作用?谢谢大家。

其他类如下:

@Configuration
public class SampleAppFactory {

    @Bean
    public Person person() {
        return new Person("Richard");
    }

    @Bean
    public SampleController sampleController() throws IOException {
        return (SampleController) loadController("/resources/fxml/Sample.fxml");
    }

    protected Object loadController(String url) throws IOException {
        InputStream fxmlStream = null;
        try {
            fxmlStream = getClass().getResourceAsStream(url);
            FXMLLoader loader = new FXMLLoader();
            loader.load(fxmlStream);
            return loader.getController();
        } finally {
            if (fxmlStream != null) {
                fxmlStream.close();
            }
        }
    }
}

public class SampleController {

    @FXML
    private Node view;
    @Autowired
    private Person person;

    public Node getView() {
        return view;
    }

    public Person getPerson() {
        return person;
    }

    public void print(ActionEvent event) {
        System.out.println("Well done, " + person.getFirstName() + "!");
    }
}

文件:fxmlapp.css


.root {
    -fx-background-color: linear-gradient(from 0% 0% to 0% 100%, #cbd0d7 0%, white 100%);
}

#printBtn {
    -fx-text-fill: #e4f3fc;
    -fx-font: 20pt "Tahoma Bold";
    -fx-padding: 10;
    -fx-base: #2d4b8e
}

#printBtn:hover{
    -fx-base: #395bae;
}

最佳答案

使用 java.net.URL 而不是 InputStream,例如如下:

protected Object loadController(String url) throws IOException {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource(url));
        loader.load();
        return loader.getController();
    }
    catch(...) {...}
}

这样 FXML 机制将能够解析相对 URI。

关于css - 如何在 JavaFx Spring 应用程序中包含 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958340/

相关文章:

JavaFx-14 resizeColumnToFitContent 方法

java - 在 javafx 应用程序中使用 HTML5 视频标签

html - 输入的样式看起来像超链接

java - 模型字段上的自定义注释,用于在 Spring Boot 中存储到数据库时进行加密和解密

java - Gmapsfx 集成在使用 Gluon-mobile 构建的 Android 应用程序中无法正常工作

java - Spring Autowiring 的成员可见性

spring - 内存数据库未创建但日志显示执行了DDL

html - 二等风格没有像预期的那样覆盖前一类的风格

html - 单击时更改div的背景颜色

jquery - 是否可以利用 `display:none` 和 `display:inline` 来取消隐藏嵌套的隐藏元素?