JavaFX CSS 解析错误

freeliver54 阅读:85 2023-09-06 19:40:43 评论:0

我收到这个错误:

com.sun.javafx.css.parser.CSSParser parse WARNING: CSS Error parsing file:/filepath/bin/: Expected LBRACE at [-1,-1]

我的代码如下:

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
 
public class Main extends Application { 
 
public static void main(String[] args) { 
    launch(args); 
} 
 
public void start(Stage primaryStage) throws Exception { 
    StackPane root = new StackPane(); 
    Scene mainScene = new Scene(root, 800, 600); 
    mainScene.getStylesheets().clear(); 
    mainScene.getStylesheets().add("//stylesheet.css"); 
    primaryStage.setTitle("Not Facebook"); 
    primaryStage.setScene(mainScene); 
    primaryStage.show(); 
    //loadGame(primaryStage); 
 
} 
 
public void loadGame(Stage primaryStage) { 
    GridPane grid = new GridPane(); 
    grid.setHgap(0); 
    grid.setVgap(0); 
    grid.setPadding(new Insets(10, 10, 10, 10)); 
    Text text = new Text("Hello"); 
    grid.add(text, 5,5); 
    Scene gameScene = new Scene(grid, 800, 600); 
    primaryStage.setScene(gameScene); 
} 
 
} 
.root { 
  -fx-background-color: #333333; 
} 

我已经搜索过,但没有人遇到同样的问题,我不知道为什么会这样。

请您参考如下方法:

您的样式表路径错误。

使用

mainScene.getStylesheets().add("stylesheet.css"); 
                               ^^ 

代替

mainScene.getStylesheets().add("//stylesheet.css"); 
                                ^^ 

请注意,您甚至不需要单个前导 / 来访问与应用程序的根类路径相关的样式表。来自JavaDoc :

The URL is a hierarchical URI of the form [scheme:][//authority][path]. If the URL does not have a [scheme:] component, the URL is considered to be the [path] component only. Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to the root of the application's classpath.


标签:CSS
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号