【问题标题】:Setting an Icon for my Java Application [duplicate]为我的 Java 应用程序设置图标 [重复]
【发布时间】:2013-11-07 08:57:12
【问题描述】:

我正在编写代码/应用程序,并且已经到了公开发布它的地步。我想知道如何为应用程序“设置”ICON 图像的简单示例代码。只是一个简单的代码,我可以放置在我的类的顶部,它将从其目录中获取图标图像 [/res/Icon.png]

谢谢

【问题讨论】:

    标签: java swing


    【解决方案1】:

    你可以使用Frame#setIconImage(Image),或者如果你想要更灵活的东西,Window#setIconImages(List)

    正如所证明的

    请感谢这些作者

    用简单的例子更新

    按其性质加载图像可能会引发问题。您需要为它可能失败的事实做好准备。希望您已将应用程序准备得足够好,以至于在正常操作下,这种情况很少见

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.Image;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FrameIconTest {
    
        public static void main(String[] args) {
            new FrameIconTest();
        }
    
        public FrameIconTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
    
                    try {
                        List<Image> icons = new ArrayList<Image>(5);
                        icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon16x16.png")));
                        icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon24x24.png")));
                        icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon32x32.png")));
                        icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon64x64.png")));
                        icons.add(ImageIO.read(getClass().getResource("/resources/FrameIcon128x128.png")));
                        frame.setIconImages(icons);
                    } catch (IOException exp) {
                        exp.printStackTrace();
                        // Log the problem through your applications logger...
                    }
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JLabel("Frame with icon"));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }
    

    【讨论】:

    • 请展开:D 我有我的代码,我只想在顶部放置一两行简单的代码来设置图标图像。我试过你发布的默认java图标仍然出现。
    • @Jahshua 示例见附件链接
    • 我这样做 ^ 但在线:ImageIO.read(imgStream);我需要用 try and catch 或 throw 声明来包围。如果我包围 try 和 catch,我需要将 myImg 变量设置为 null,如果我抛出 iOS Exception 之类的声明,它会破坏整个代码。
    • 因此,无论您如何加载图像,您都需要处理可能引发异常的可能性。您如何处理将取决于您的需求
    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多