首页 文章

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

提问于
浏览
0

当我在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;
}

1 回答

  • 1

    使用a 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 .

相关问题