当样式表通过 fxml 链接时,JavaFX tooltop 样式类将不适用

标签 java css javafx tooltip fxml

我的控件类中有一个函数如下,我已将 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/

相关文章:

java - 动态添加内容到菜单

java - 小程序查看问题

java - 根据条件插入到 hibernate 审计表

java - Android:获取通知的唯一ID

javascript - 自定义模态脚本 - Safari 和 IE 问题

JavaFX ImageView 图像未显示

multithreading - 为什么javafx应用程序不是线程安全的?

java - 如何排除某些节点

html - 平板电脑和手机的缩放图像

css - 字体大小尽可能大以适合窗口宽度