如何更改 JFrame 中的 java 图标 [英] How to change java icon in a JFrame

查看:66
本文介绍了如何更改 JFrame 中的 java 图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我一直在安静地研究这个.我对java相当陌生,但认为这很容易.我已经尝试了几乎所有已在此站点上回答的方法,但仍然没有运气,通常当我在这里查看时,我能够找到适合我正在寻找的答案.有谁知道如何更改 JFrame 上角的 Java 图标.我很肯定它不是我的文件路径,因为我所有的图像都在同一个文件夹中并且它们都可以工作,这是唯一一个我似乎无法工作的.

Ok so I've been researching this one quiet a bit. I am fairly new to java but thought that this one would be easy. Ive tried just about every way that has been answered on this site and still no luck, and usually when I look here I am able to find a answer that fits what I am looking for. Does anyone know how to change the Java icon in the top corner of the JFrame. I'm pretty positive that its not my file path either because all my images are in the same folder and they all work, this is the only one that I can't seem to get to work.

这是我的程序主菜单代码的第一部分,除了我尝试添加图标图像时,一切正常.我在下面输入的代码中没有 JFrame IconImage 的任何内容,我删除了它,因为它不起作用.因此,如果有人知道如何使用此代码,将不胜感激,非常感谢您!

This is the first part my code for the main menu of my program, everything works except when i try to add the icon image. The code I've entered below does not have anything in it for the JFrame IconImage, I removed it since it didn't work. So if there is someone who knows how to get it working with this code that would be highly appreciated, thank you very much in advanced!

public class MainFrame
{
private MyPanel main;
private MyPanel2 create;
private MyPanel3 update;
private MyPanel4 find;
JFrame frame = new JFrame("Main Menu:");

public void displayGUI()
{
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    contentPane.setLayout(new CardLayout());
    main = new MyPanel(contentPane, this);
    create = new MyPanel2(contentPane);
    update = new MyPanel3(contentPane);
    find = new MyPanel4(contentPane);
    contentPane.add(main, "Main Menu");
    contentPane.add(create, "Create Part");
    contentPane.add(update, "Update Part");
    contentPane.add(find, "Find Part");
    frame.setLocation(200, 200);
    frame.setSize(700, 580);
    frame.setContentPane(contentPane);

    frame.setVisible(true);

}

推荐答案

我有一个答案给你.首先,确保图像位于文件夹中,而不是包中.接下来,插入这行代码:

I have an answer for you. First, make sure that the images are in a folder, not a package. Next, insert this line of code:

Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("path/to/image.png"));
ImageIcon icon = new ImageIcon( );
setIconImage(icon.getImage());

此代码从类路径中获取图像,并将其作为图像图标返回,然后设置它.这应该将图像图标添加到应用程序.如果没有,请告诉我.

This code gets the image from the class path, and returns it as a image icon, and then it sets it. This should add the image icon to the application. If it doesn't, then tell me.

在你告诉我那不起作用之后,我决定再试一次......首先,将您的图像放入一个完全独立的文件夹中.我通常称之为/res.接下来,将您的图像放在那里.现在,为了加载,我采取了完全不同的路线.我决定使用 ImageIO 而不是默认加载.要加载图像,请使用以下代码:

After you told me that that didn't work then I decided to take a second crack at it... First, put your images into a completely separate folder. I usually call this /res. Next, put your image in there. Now, for loading I took a completely different route. I decided to use ImageIO instead of default loading. To load the image, you use this code:

try {
    frame.setIconImage(ImageIO.read(new File("res/icon.png")));
}
catch (IOException exc) {
    exc.printStackTrace();
}

ImageIO 更适合加载图像.如果这仍然不起作用,请告诉我.

ImageIO works a lot better for loading images. If this still doesn't work then please tell me.

如果您想将其导出为 JAR,请将与您在程序中使用的名称相同的文件夹放在与 JAR 相同的目录中.

If you want to export this as a JAR then put a folder the same name as you used in the program in the same directory as the JAR.

这篇关于如何更改 JFrame 中的 java 图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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