繁体   English   中英

JavaFX-工具栏按钮上的图标消失

[英]JavaFX - disappearing icons on toolbar buttons

我正在JDK 8上编写JavaFX应用程序。该应用程序具有一个菜单和带有一些按钮的工具栏。 问题是,一旦菜单打开并覆盖了工具栏按钮,收费栏按钮上的图标就会消失。 按钮功能保持不变,工具提示正确显示,仅缺少图标。 下面的图像显示了这一点:

在此处输入图片说明

我在UI线程之外创建工具栏按钮,但将它们添加到UI线程上的工具栏。

 // this is off UI thread
 newFileButton = new Button();
 newFileButton.setId(JFXFileMenuConstants.NEW_FILE_TOOLBAR_BUTTON);
 newFileButton.setGraphic((Node) handler.getResource("jfx.gui.file.new.menuitem.icon"));
    newFileButton.setTooltip(new Tooltip(handler.getString("jfx.gui.toolbar.newfile.tooltip")));
 .........
   ToolBar filesToolbar = new ToolBar(newFileButton, openFileButton, separator, saveFileButton, saveasFileButton);

  Platform.runLater(() -> {
        toolbarContainer.getChildren().add(toolbar);
    });

知道是什么原因造成的吗?

好吧,要回答我自己的问题,问题出在资源加载中。 我正在将图标作为ImageView而不是Image加载。 在资源包中,我有

 {"jfx.gui.file.new.menuitem.icon", new ImageView(this.getClass().getClassLoader().getResource("/icons/filenew.png").toString()) },

当我将其更改为

 {"jfx.gui.file.new.menuitem.icon", new Image(this.getClass().getClassLoader().getResource("/icons/filenew.png").toString()) },

这行:

newFileButton.setGraphic((Node) handler.getResource("jfx.gui.file.new.menuitem.icon"));

newFileButton.setGraphic(new ImageView((Image)handler.getResource("jfx.gui.file.new.menuitem.icon")));

一切正常。 非常感谢大家的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM