简体   繁体   中英

Can't load resource for Icon

I'm setting up a simple GUIand I got stuck trying to loading an image for a button.

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. So what am I doing wrong?? it gives me a NullPointerException no matter how I write the URL. The image is under a "res" folder in my project..

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..

From java.lang.Class#getResource API documenation:

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 '/'. 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 '.' ('\.').

If your image is under "res" folder (under project root) you need a slash and the path should look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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