【问题标题】:JavaFX - adding css file to Maven projectJavaFX - 将 css 文件添加到 Maven 项目
【发布时间】:2018-01-04 16:41:30
【问题描述】:

我刚开始在 javafx 中编写我的第一个简单 gui,我正在尝试将 css 文件添加到 javafx 应用程序。当它不是 maven 项目时,它工作正常。当涉及到maven时,它找不到文件。我也尝试过将整个路径复制到文件中,没有区别。

我也使用了scene.getStylesheets().add("SceneStyle.css");,这只是另一个错误。

我也很感激所有关于设计的意见。将所有场景包含在 1 类中是否可以,或者我应该将其划分为更多文件?如果是这样,它们之间如何通信。

主窗口

public class MainWindow extends Application
{
private Stage window;
private MenuBar menuBar;

private BorderPane mainLayout;
private VBox entryLayout;
private HBox lobbyLayout;


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

public void start (Stage primaryStage) throws Exception
{
    window = primaryStage;

    mainLayout = new BorderPane();

    prepareMenuBar();
    mainLayout.setTop(menuBar);
    BorderPane.setMargin(menuBar,new Insets(0,0,20,0));

    prepareEntryLayout();
    mainLayout.setCenter(entryLayout);

    Scene scene = new Scene(mainLayout,300,200);
    scene.getStylesheets().add(getClass().getResource("SceneStyle.css").toExternalForm());


    window.setScene(scene);
    window.show();
}

private void prepareMenuBar()
{
    menuBar = new MenuBar();
    Menu options, gameRules, info;

    options = new Menu("Opcje");
    MenuItem display = new MenuItem("Wyświetlanie");
    MenuItem connection = new MenuItem("Połączenie");
    options.getItems().addAll(display,connection);

    gameRules = new Menu("Zasady Gry");
    info = new Menu("Info");

    menuBar.getMenus().addAll(options, gameRules, info);
}

private void prepareEntryLayout()
{
    entryLayout = new VBox();
    Label nicknameLabel = new Label("Pseudonim :");
    TextField userInput = new TextField();
    Button nextScene = new Button("nextScene");

    Board b = new Board();
    b.createPieces();

    nextScene.setOnAction(e -> {
        mainLayout.setCenter(boardPrint(b));
    });

    entryLayout.getChildren().addAll(nicknameLabel, userInput, nextScene);

}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.chinesecheckers.client</groupId>
    <artifactId>client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

</project>

SceneStyle.css

.root {
    -fx-background-color: #FFF8DC;
}

编辑: 我正在使用 Intellij,这是项目结构

-src
--main
---java
----GUI
-----MainWindow.java
-----SceneStyle.css
-pom.xml

【问题讨论】:

  • 尝试放置 main/java/GUI/SceneStyle.css 而不仅仅是 SceneStyle.css
  • main/java/GUI/SceneStyle.css和SceneStyle.css没有区别

标签: java css maven javafx


【解决方案1】:

如果 .css 文件在其中,您是否检查过生成的 jar?

我把 .css 放在资源中

理想的情况是为每个场景创建一个类,但如果一个类中需要多个类,我不明白为什么不这样做。

【讨论】:

  • 你是对的 - jar 没有它。如何向其中添加 .css 文件?
  • 按照他的结构,你需要添加资源目录,里面你需要创建与你的包相同的目录结构
猜你喜欢
  • 2022-01-24
  • 2016-04-06
  • 2022-12-03
  • 2021-03-05
  • 1970-01-01
  • 2021-07-11
  • 2023-03-18
  • 2020-10-27
  • 2019-02-12
相关资源
最近更新 更多