【问题标题】:JavaFX 2 Window Icon not working [closed]JavaFX 2窗口图标不起作用[关闭]
【发布时间】:2015-01-16 23:34:26
【问题描述】:

我正在尝试向我的 JavaFX 2 应用程序添加一个图标,但我发现的方法似乎不起作用。

Image icon = new Image(getClass().getResourceAsStream("/images/icon.png"));
stage.getIcons().add(icon);

图标大小为 32x32。

当我尝试时

Image icon = new Image("http://goo.gl/kYEQl");

在 Netbeans 和可运行的 jar 中确实有效。

我希望可以解决这个问题。

【问题讨论】:

  • 您确定正在加载您的图片吗?
  • 如果我对文档的理解正确的话,资源流是多余的,你可以直接使用文件路径。无论如何,请确保图像存在于您再次提供的位置..
  • 是的,因为当我删除“images”前面的“/”或使用“/images/icon2.png”时,我得到一个 NullPointerException,指出 Inputstream 不能为空。
  • @subash 我试过了,但它似乎没有显示图标。

标签: java jar icons javafx-2 executable-jar


【解决方案1】:

问题出在图标本身。它确实按应有的方式加载了它,但由于某种原因它没有按应有的方式显示。

我将尝试使用的图标重新制作成不同的尺寸(16x16 到 512x512),并将它们全部添加到图标列表中。

stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_16.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_32.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_64.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_128.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_256.png")));
stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo_512.png")));

现在它可以正常使用图标了。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 2011-06-21
    • 2018-03-05
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2013-12-14
    相关资源
    最近更新 更多