简体   繁体   English

如何在JFrame中更改java图标

[英]How to change java icon in a JFrame

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. 我对java很新,但认为这个很容易。 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. 有谁知道如何更改JFrame顶角的Java图标。 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. 我非常肯定它不是我的文件路径,因为我的所有图像都在同一个文件夹中并且它们都工作,这是我唯一无法工作的。

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. 我在下面输入的代码中没有任何内容用于JFrame IconImage,我将其删除,因为它不起作用。 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. 如果没有,请告诉我。

EDIT: 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. 我通常称之为/ 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. 我决定使用ImageIO而不是默认加载。 To load the image, you use this code: 要加载图像,请使用以下代码:

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

ImageIO works a lot better for loading images. ImageIO可以更好地加载图像。 If this still doesn't work then please tell me. 如果这仍然不起作用,那么请告诉我。

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. 如果要将其导出为JAR,则将与您在程序中使用的名称相同的文件夹放在与JAR相同的目录中。

For example in a NetBeans project, create a resources folder in the src folder. 例如,在NetBeans项目中,在src文件夹中创建资源文件夹。

Put your images (jpg, ...) in there. 把你的图像(jpg,...)放在那里。

Whether you use ImageIO or Toolkit (including getResource), you must include a leading / in your path to the image file: 无论您使用ImageIO还是Toolkit (包括getResource),都必须在图像文件的路径中包含一个前导/:

Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/agfa_icon.jpg"));
setIconImage(image);

If this code is inside your JFrame class, the image is added to the frame as an icon in your title bar. 如果此代码位于JFrame类中,则图像将作为标题栏中的图标添加到框架中。

This works pretty fine for me. 这对我来说非常好。 Just add this after you've created your JFrame . 只需在创建JFrame后添加即可。

try {
   Image image = new ImageIcon("/icons/image.jpg").getImage();
   frame.setIconImage(image);
}catch(Exception e){
   System.out.println("Application icon not found");
}
  1. Paste your image icon (fav.png) in the same package first, 首先将图像图标(fav.png)粘贴到同一个包中,
  2. Write following code in constructor of JFrame : JFrame的 构造函数中编写以下代码:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("fav.png"))); setIconImage(Toolkit.getDefaultToolkit()的getImage(的getClass()的getResource( “fav.png”)));

Note:- fav.png is the name of icon 注意: - fav.png是图标的名称

this.setIconImage(new ImageIcon(getClass().getResource("/iconsfolder/iconsname.jpg")).getImage()); 
          // sets the Global icon for the system

try this code put after this code: 尝试此代码后放置此代码:

public void displayGUI()
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM