2014-11-03 40 views
0

我遇到问题,因为某些恶意原因我无法使用第二个css文件。我使用IntelliJ IDEA 14(版本139.222)终极版。 我使用JavaFX 2.0它内置在IntelliJ IDEA 139.222内部版本 Board.css不起作用。 LoginForm.css工作正常。 另外我已经将; * .css资源模式添加到编译器部分。JavaFX - css在我的项目中不起作用

btn是我点击是在登录表按钮。 代替GameApp类(它是从应用类继承的类), 我用的getClass()方法,甚至一些其他类。没有结果。

我试图在Board.css使用的CSS是.button:悬停{-fx背景色:白色;} }

下面的代码:

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; 
} 
+0

哪个版本的JavaFX你使用?在早期版本中,将样式表应用于'Parent'有一些问题。假设您没有看到任何错误消息,请尝试将样式表添加到“Scene”而不是根节点。 – 2014-11-03 13:58:20

+0

我不知道。 JavaFX内置于Intellij IDEA中。我正在处理这一个星期:)。让我检查一下。哦,我在Windows 8下运行的JavaFX Scene Builder 2.0中创建了一些FXML文件。 – 2014-11-03 14:18:45

+0

如何做到这一点? – 2014-11-03 14:21:49

回答

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(); 
     } 
    } 
}); 
+0

现在我将样式表直接添加到Scene对象中。那是你的意思?尝试{Pane pane =(Pane)FXMLLoader.load(GameApp.class.getResource(“/ sample/buttonGrid.fxml”)); 场景场景=新场景(窗格); scene.getStylesheets()。add(“/ sample/Board.css”); currentStage.setScene(scene); currentStage.setResizable(false); currentStage.show(); } – 2014-11-03 15:45:54

+0

我使用JDK 1.8.0_25和JavaFX 2.0。 – 2014-11-03 15:49:38

+0

这似乎不太可能,除非你有一个非常奇怪的设置。 Java 8(JDK 1.8.0)已经包含了JavaFX 8。你能做'System.out.println(System.getProperty(“javafx.version”));'来确认。如果您真的使用JavaFX 2.0,则需要修复您的项目设置。 – 2014-11-03 16:17:58