简体   繁体   English

无法加载图标资源

[英]Can't load resource for Icon

I'm setting up a simple GUIand I got stuck trying to loading an image for a button. 我正在设置一个简单的GUI,但在尝试为按钮加载图像时遇到了麻烦。

public class Client extends JFrame{

    private JTextField field;
    private JLabel label;
    private JButton send;
    private Socket socket;

    Client(){
        super("Messenger");
        try {
            socket=new Socket("localhost",65535);
        } catch (IOException e1) {
            System.out.println("can't estabilish connection");
            return;
        }
        setLayout(new FlowLayout());
        label=new JLabel("insert text here");
        add(label);
        field=new JTextField(20);
        add(field);
        ImageIcon ico=new ImageIcon(getClass().getResource("res/richard.png"));
        send=new JButton("send",ico);
        send.setFocusPainted(false);
        add(send);
        send.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            OutputStream out=socket.getOutputStream();
                            String s=field.getText();
                            if (s.equals(".")) {
                                out.write(s.getBytes());
                                socket.close();
                                System.exit(0);
                            }
                            out.write((s+"\n").getBytes());
                            field.setText("");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        pack();
        setLocation(500, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new Client();
    }

}

it's a simple client for a messanging app, but I can't get the image to show on the button. 这是一个简单的客户端应用程序,但是我无法将图像显示在按钮上。 I'm using getResource() instead of the ImageIcon constructor because it wasn't showing in the Jar if I used that. 我使用的是getResource()而不是ImageIcon构造函数,因为如果使用它,它就不会显示在Jar中。 So what am I doing wrong?? 那我在做什么错? it gives me a NullPointerException no matter how I write the URL. 无论我如何编写URL,它都会为我提供NullPointerException。 The image is under a "res" folder in my project.. 该图像位于我的项目中的“ res”文件夹下。

this is the stack trace: 这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Client.<init>(Client.java:27)
    at Client.main(Client.java:58)

it originates (as expected) in the ImageIcon constructor.. 它起源于(如预期的)ImageIcon构造函数。

From java.lang.Class#getResource API documenation: java.lang.Class#getResource API文档中:

an absolute resource name is constructed from the given resource name using this algorithm: 使用以下算法从给定资源名称构造绝对资源名称:

If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'. 如果名称以'/' ('\\ u002f')开头,则资源的绝对名称是名称中'/'之后的部分。 Otherwise , the absolute name is of the following form: modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' 否则 ,绝对名称的格式如下: modified_pa​​ckage_name / name,其中Modifyed_pa​​ckage_name是此对象的程序包名称,用“ /”代替“。”。 ('\.'). ('\\ u002e')。

If your image is under "res" folder (under project root) you need a slash and the path should look like: 如果图像在“ res”文件夹(在项目根目录下)下,则需要一个斜杠,并且路径应类似于:

new ImageIcon(getClass().getResource("/res/richard.png"));

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

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