设置 JFrame 图标 - 无法在先前工作的线程中获得建议的解决方案

问题描述

我正在尝试设置 JFrame 图标。我已经尝试了所有建议的解决方herehere ,但还没有成功。

在我下面的方法中,您可以看到所有尝试的解决方案:

1 和 2 没有设置图标(我仍然看到咖啡杯)。

3 和 6 得到这个错误

The method setIconImage(Image) is undefined for the type Icon 

4 收到此错误

java.lang.NullPointerException

5 获得:

Type mismatch: cannot convert from URL to DocFlavor.URL

我的电话课程在这里

/Users/laWrence/eclipse-workspace/COA_Application/src/main/java/misc/Icon

我的图片在这里

/Users/laWrence/eclipse-workspace/COA_Application/logo.png 

(我也试过COA_Application/src/main/resources/logo.png

我是初学者,如果我的速度慢,请见谅。另请注意,我使用的是 mac。

package misc;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;

import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.google.api.services.sheets.v4.model.Color;

public class Icon {
    
    static String filepath = "/logo.png";
    
    public void showFrame() throws IOException {
        
        JFrame frame = new JFrame("Icon frame");
        
        //method 4
    frame.setIconImage(Toolkit.getDefaultToolkit().getimage(this.getClass().getResource(filepath)));
        
         //method 1
        //BufferedImage myPicture = ImageIO.read(new File(filepath));
        //frame.setIconImage(myPicture);
        
        //method 2
        //frame.setIconImage(ImageIO.read(new File(filepath)));
        
        //method 3
        //setIconImage(new ImageIcon(filepath).getimage());
        
        //method 5
        //URL url = getClass().getResource(filepath);
        //frame.setIconImage(imgicon.getimage());
        
        //method 6
        //ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
        //setIconImage(img.getimage());
        
        JPanel panel = new JPanel();
        frame.add(panel);
        
        frame.pack();
        frame.setSize(new Dimension(600,600));
        frame.setLocationRelativeto(null);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) throws Exception {
        
        Icon obj = new Icon();
        obj.showFrame();
    }
}

图片

My icon

解决方法

应用程序资源将在部署时成为嵌入式资源,因此明智的做法是立即开始访问它们。 必须通过 URL 而不是文件访问。有关如何形成 URL 的信息,请参阅 info page for embedded resource

当然,细节决定成败。用于路径和资源名称的(大写/小写)大小写必须与文件系统上的完全相同。使 getResource(..) 生效的最简单方法是使用类路径根目录下的路径。通过在路径前加上 / 来指定“从根”。

使用最新代码的示例(从 URL 加载图标):

import java.awt.*;
import javax.swing.*;
import java.net.*;

public class Icon {
    
    public void showFrame() throws Exception {
        JFrame frame = new JFrame("Icon frame");
        
        URL url = new URL("https://i.stack.imgur.com/bQ8fP.png");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
        
        frame.pack();
        frame.setSize(new Dimension(400,100));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) throws Exception {
        Icon obj = new Icon();
        obj.showFrame();
    }
}

enter image description here

编辑:

鉴于它在这里完美运行,我开始认为问题在于所使用的特定 JRE。

我刚刚构建并安装了我的应用程序,它有正确的图标!