Java Swing 或 Java FX 是否应该使用特定的图标格式,如果是,我该如何包含它

Is there a specific icon format that I should use for Java Swing or Java FX, and if so how do I include it

我最近在 blender 中为我的应用程序设计了一个图标。我已将其导出为 600 x 600 的方形 png 文件。我如何将其放入 Java Swing 或 JavaFX 应用程序中,我是否需要将其转换为不同的格式?我还需要将它整齐地打包到一个 .JAR 文件中。该图标需要在所有支持 JRE 的平台上正确显示。谢谢

frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png"))); 这应该将图像添加到框架中,如果您制作一个新面板并将其添加到框架中,您也可以执行 panel.add()

Java 的 ImageIO class is a handy tool for reading and writing images. It supports JPEG, PNG, BMP, WBMP, and GIF formats. You can also use some third party dependencies like TwelveMonkeys to add support for more formats。这就是我在 Swing 应用程序中实现它的方式:

JFrame frame = new JFrame("My Window");

// Setup frame...

try (InputStream in = this.getClass().getResourceAsStream("/myPackage/mySubPackage/myIcon.png")) {
    BufferedImage image = ImageIO.read(in);
    frame.setIconImage(image);
} catch (IOException e) {
    e.printStackTrace();
}

frame.setVisible(true);