如何在FXML中添加CSS样式表

cod*_*ody 11 css javafx stylesheet fxml scenebuilder

我想将css文件链接到我的应用程序.在我的fxml文件中,我使用:

  <stylesheets>
    <URL value="@../stylesheet1.css" />
  </stylesheets>
Run Code Online (Sandbox Code Playgroud)

...当我在scenebuilder中打开fxml文件时,我可以看到样式预览.但是当我尝试运行应用程序时,我收到一个错误:

java.net.MalformedURLException:无协议:../ styleles1.css

所以我用这种方式测试了它:

<stylesheets>
    <String fx:value="stylesheet1.css" />
</stylesheets>
Run Code Online (Sandbox Code Playgroud)

现在它是另一回事 - 应用程序启动并应用css,但我没有在scenebuilder中看到预览.错误消息:

"文件stylesheet1.css不存在.找不到资源stylesheet1.css."

那么如何正确附加css文件呢?


好吧,虽然我的问题没有得到解答,为什么它不能以上述方式工作,但我找到了一个适合我的解决方案.在我的FXML中我只有这一行

<?scenebuilder-stylesheet ../stylesheet1.css?>
Run Code Online (Sandbox Code Playgroud)

所以Scenebuilder与那个css一起工作.在我的主类中,我以编程方式设置样式表:

Scene scene = new Scene(root);
String css = this.getClass().getResource("../stylesheet1.css").toExternalForm(); 
scene.getStylesheets().add(css);
Run Code Online (Sandbox Code Playgroud)

小智 12

我发现包含css文件的可用和工作解决方案fxml 是添加 stylesheets="@app/cssfilename.css"到文件的父节点,fxml就像堆栈窗格一样

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.fxml.SettingsController" stylesheets="@app/cssfilepath.css">

......
.....
.....
</StackPane>
Run Code Online (Sandbox Code Playgroud)


Rol*_*and 8

这是一个适用于开发环境,Scene Builder和打包JAR的解决方案.

文件夹结构:

在此输入图像描述

Main.java:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/RootLayout.fxml"));
            AnchorPane rootLayout = (AnchorPane) loader.load();

            Scene scene = new Scene(rootLayout, 400, 400);
            scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm());

            primaryStage.setScene(scene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

RootLayout.fxml:

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

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

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.RootLayoutController">
   <children>
      <Pane layoutX="0.0" layoutY="0.0" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button fx:id="sunButton" layoutX="74.0" layoutY="88.0" mnemonicParsing="false" onAction="#handleSunButtonClick" styleClass="sun-button" stylesheets="@../css/toolbar.css" text="Button" />
         </children>
      </Pane>
   </children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)

RootLayoutController.java:

package application.view;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class RootLayoutController {


    @FXML
    Button sunButton;

    @FXML
    public void handleSunButtonClick() {
        System.out.println( "Button clicked");
    }
}
Run Code Online (Sandbox Code Playgroud)

toolbar.css:

.sun-button {
  -fx-graphic: url('./icons/sun.png');
}
Run Code Online (Sandbox Code Playgroud)

application.css:

.root {
    -fx-background-color:lightgray;
}
Run Code Online (Sandbox Code Playgroud)

sun.png:

在此输入图像描述

这既适用于开发环境,也适用于打包JAR(在Eclipse中选择"将所需库提取到生成的JAR中").

屏幕截图(只是通过css加载图标的按钮)

在此输入图像描述


DaR*_*ich 5

如果您不想以编程方式执行此操作,则可以通过分离代码和资源来执行此操作。我有一个像这样的 Maven 项目结构,但这不是必需的。

src/main/java/${packageName}
    MainWindow.fxml
src/main/resources/${packageName}
    stylesheet1.css
Run Code Online (Sandbox Code Playgroud)

现在您可以使用样式表.fxml

stylesheets="@stylesheet1.css"
Run Code Online (Sandbox Code Playgroud)

或者像您一样使用样式表标签。

可能会有警告,SceneBuilder但它有效。