为我的Java应用程序设置一个图标 [英] Setting an Icon for my Java Application

查看:135
本文介绍了为我的Java应用程序设置一个图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am in the middle of a code/application and I've gotten to the point where I am releasing it publicly. Id like to know a simple example code of how I can "set" the ICON image for the application. Just a simple code where I can place at the top of my class that will grab the Icon image out of its directory [/res/Icon.png]

谢谢< 3

推荐答案

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

You can use Frame#setIconImage(Image) or if your want something that is a little more flexible, Window#setIconImages(List)

如所示by

  • Set Icon Image in Java
  • How to add an image to a JFrame title bar?

请作者

使用简单示例更新

按图片加载图片的性质可以提出问题。你需要为它可能失败的事实做好准备。希望你已经准备好了应用程序,在正常操作下,这将是一种罕见的情况

Loading images by it's nature can raise problems. You need to be prepared for the fact that it might fail. Hopefully, you've prepared the application well enough that under normal operations, this would be a rare situation

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);
            }
        });
    }

}

这篇关于为我的Java应用程序设置一个图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆