JavaFX - css在我的项目中不起作用

时间:2014-11-03 13:33:23

标签: java css javafx

当我因某些邪恶的原因无法使用第二个css文件时,我遇到了问题。 我使用 IntelliJ IDEA 14(build 139.222)Ultimate Edition 。 我使用内置在IntelliJ IDEA 139.222版本中的 JavaFX 2.0 Board.css 不起作用。 LoginForm.css 工作正常。 此外,我已将;?*。css资源模式添加到编译器部分。

btn 是我点击登录表单的按钮。 而不是 GameApp 类(它是继承自应用程序类的类), 我使用 getClass()方法甚至其他一些类。没有结果。

我试图在Board.css中使用的CSS是 .button:hover {-fx-background-color:white;} }

以下是代码:

    btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        Parent root = null;
        try {
            root = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
            root.getStylesheets().add(GameApp.class.getResource("/sample/Board.css").toExternalForm());
            currentStage.setScene(new Scene(root));
            currentStage.setResizable(false);
            currentStage.show();

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

@Override
public void start(Stage primaryStage) throws InterruptedException, IOException {
    this.currentStage = primaryStage;
    primaryStage.setTitle("Hello World");
    initPane();
    Scene scene = new Scene(grid, 698, 364);
    scene.getStylesheets().add(GameApp.class.getResource("LoginForm.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();

}

这个也不起作用:

        try {
            Pane pane = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
            pane.getStylesheets().add("/sample/Board.css");
            currentStage.setScene(new Scene(pane));
            currentStage.setResizable(false);
            currentStage.show();

        }

Board.css是:

.button:hover {
     -fx-background-color: white;
 }

1 个答案:

答案 0 :(得分:0)

早期版本的JavaFX在向Parent个节点添加样式表时遇到了一些问题,而不是直接将它们添加到Scene。按钮的操作处理程序中的代码将样式表添加到节点。

我建议您确保使用最新版本的JDK和JavaFX;最好是Java 8(与JavaFX 8捆绑在一起)。如果您被迫使用Java 7,请确保您拥有与JavaFX 2.2捆绑在一起的最新版本。只需更新JDK版本就可以解决问题。

还尝试将样式表直接添加到场景中,而不是添加到根节点:

btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        Parent root = null;
        try {
            root = (Pane) FXMLLoader.load(GameApp.class.getResource("/sample/buttonGrid.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(GameApp.class.getResource("/sample/Board.css").toExternalForm());
            currentStage.setScene(scene);
            currentStage.setResizable(false);
            currentStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});
相关问题