【发布时间】: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