我的控件类中有一个函数如下,我已将 css 文件与 fxml 链接起来。但是相关的样式类没有得到应用,如果我不手动加载样式表的话。
private void ValidateRequired(TextField field){
Tooltip errorm = new Tooltip("This is required");
errorm.getStyleClass().removeAll();
Scene scene = field.getScene();
scene.getStylesheets().add(this.getClass().getResource("../css/sale.css").toExternalForm());
//If the above line of code is commented out style will not be applied.
errorm.getStyleClass().add("error");
Point2D p = field.localToScene(0.0, 0.0);
errorm.show((Stage)field.getScene().getWindow(),p.getX()
+ field.getScene().getX() + field.getScene().getWindow().getX(), p.getY()
+ field.getScene().getY() + field.getScene().getWindow().getY()+field.getHeight()+2);
}
我在这里找到了确切的问题 JavaFX Tooltip customisation .但它没有提供任何解决问题的方法,并且大约在 3 年前被问到。这里我已经链接了 fxml 中的 css 文件。
<GridPane prefHeight="1031.0" prefWidth="821.0" stylesheets="@../css/Sale.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BikeShop.control.SaleControl">
最佳答案
请在src包后面写完整路径。例如;
<GridPane prefHeight="1031.0" prefWidth="821.0" stylesheets="@/com/test/css/Sale.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BikeShop.control.SaleControl">
关于当样式表通过 fxml 链接时,JavaFX tooltop 样式类将不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42950126/