【问题标题】:Custom CSS for JavaFX virtual keyboard will not loadJavaFX 虚拟键盘的自定义 CSS 将不会加载
【发布时间】:2015-09-08 15:50:04
【问题描述】:

在使用 FXVK 虚拟键盘时,我想根据自己的喜好更改默认皮肤。我在 JavaFX 的源代码中找到的 css 标签。这些已添加到自定义 css 并加载,如下所示。

public static void setVirtualKeyboardCSS() {
  @SuppressWarnings("deprecation")
  final Iterator<Window> windows = Window.impl_getWindows();

  while (windows.hasNext()) {
      final Window window = windows.next();
      if (window instanceof PopupWindow) {
          if (window.getScene() != null && window.getScene().getRoot() != null) {
              Parent root = window.getScene().getRoot();
              if (root.getChildrenUnmodifiable().size() > 0) {
                  Node popup = root.getChildrenUnmodifiable().get(0);
                  if (popup.lookup(".fxvk") != null) {
                      if (popup instanceof FXVK) {

                          FXVK keyboard = (FXVK)popup.lookup(".fxvk") // reference to the vk skin
                          ObservableList<String> sheets = keyboard.getStylesheets();
                          sheets.add("@customVK.css");
                          System.out.println("Setting keyboard stylesheet");
                      }
                  }
              }
          }
      }
  }
}

当期望显示键盘时,调用此函数,输出显示调用已完成。然而,CSS 不会改变布局。使用 keyboard.getScene().getStyleSheets() 而不是 keyboard.getStyleSheets() 也没有提供可行的替代方案。

【问题讨论】:

  • 我猜 .css 应该在应用程序启动时加载,而不是在函数的某个地方。因为 javafx 使用的是默认样式表,比如 modena.css。
  • 据我所知,用于位置解析的@ 语法仅在 FXML 中有效。 (我可能是错的;我从未在 Java 中尝试过,但在这里没有任何意义)。通常你会做类似sheets.add("customVK.css")sheets.add(getClass().getResource("customVK.css").toExternalForm()) 的事情。
  • 最后一个选项按要求工作,谢谢。显然,getClass() 调用不能使用静态函数开箱即用,但这很容易解决。 CSS 必须直接在键盘上设置(据我了解),因为键盘的弹出是一个单独的阶段。

标签: css javafx keyboard virtual


【解决方案1】:

您的代码为我工作了很多,但是,您的错误是您必须在焦点事件通过应用程序传播并且 FXVK 子窗口完全呈现后调用“setVirtualKeyboardCSS”函数。

public static final int TICK = 5;

public void setTimer() {
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                Platform.runLater(() -> {
                    setVirtualKeyboradCss();
                });
            }
        }, TICK, TICK);
    }

    public void setVirtualKeyboradCss() {
        @SuppressWarnings("deprecation")
        final Iterator<Window> windows = Window.impl_getWindows();

        while (windows.hasNext()) {
            final Window window = windows.next();
            if (window instanceof Popup) {
                String vars = getClass().getResource("/css/variables.css").toExternalForm();
                String embed = getClass().getResource("/css/embeded.css").toExternalForm();

                FXVK keyboard = (FXVK) window.getScene().getRoot().lookup(".fxvk");
                if (keyboard != null) {
                    if (!keyboard.getStylesheets().contains(embed)) {
                        keyboard.getStylesheets().addAll(vars, embed);
                    }
                }

                timer.cancel();
                timer = null;
            }
        }
    }

变量.css

@font-face {
    -fx-font-family: "DINPro-Bold";
    src: url('/fonts/DINPro-Bold.otf');
}

* {
    -color-black: #303030;
    -color-white: #FFFFFF;
    -color-brownish-gray: #606060;
    -color-yellow: #FCB400;
}

embeded.css

.fxvk {
    -fx-background-color: -color-brownish-gray;
}

.fxvk .key{
    -fx-font-family: "DINPro-Bold", "sans-serif";
    -fx-border-image-source: null;
    -fx-border-radius: 3;
    -fx-background-radius: 3;
    -fx-background-color: radial-gradient(center 50% 50%, radius 60%, derive(-color-white, -65%), -color-black);
}

.fxvk .key:hover{
    -fx-background-color: -color-yellow;
}

.fxvk-secondary{
}

.shift-icon {
}

.capslock-icon{
}

.shift{
    -fx-fill: -color-white;
}

.special {
    -fx-fill: -color-white;
}

.backspace{
    -fx-fill: -color-white;
}

.enter {
    -fx-fill: -color-white;
}

.hide{
    -fx-fill: -color-white;
}

.multi-char-text{
    -fx-fill: -color-white;
}

.key-text {
    -fx-fill: -color-white;
    -fx-font-family: "DINPro-Bold", "sans-serif";
}

.key-alttext{
    -fx-fill: -color-white;
    -fx-font-family: "DINPro-Bold", "sans-serif";
}

.key-icon {
    -fx-fill: -color-white;
}

.multi-char-text{
    -fx-fill: -color-white;
}

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2015-01-02
    • 2015-01-02
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多