How to put an image on a JButton?

吃可爱长大的小学妹 提交于 2019-12-10 17:17:56

问题


I am writing a program that requires I have a button with an image over top of it, however, so far, I have not been able to get it to work. I have checked several other posts on this site, including How do I add an image to a JButton.
My Code:

public class Tester extends JFrame
{
    public Tester()
    {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);

        setTitle("Image Test");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton button = new JButton();
        try 
        {
            Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));
            button.setIcon(new ImageIcon(img));
        } 
        catch (IOException ex) {}

        button.setBounds(100,100,100,100);
        panel.add(button);
    }

    public static void main(String[] args)
    {
        Tester test = new Tester();
        test.setVisible(true);
    }
}

When this code runs, an error results: Exception in thread "main" java.lang.IllegalArgumentException: input == null! This error occurs at the line:

Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

I don't think that this error is due to the file not being found by java code, as my Images folder is in the src folder (I am using Eclipse) as recommended by the above link.
Does anyone have any ideas to what the problem may be?
Thanks.


回答1:


While using Eclipse, you don't keep your images into src folder instead you create a Source Folder for this purpose. Please refer to this link regarding how to add images to resource folder in Eclipse.




回答2:


Use this to create the button.

JButton button = new JButton(new ImageIcon(getClass().getClassLoader()
                                          .getResource("Images/BBishopB.gif")));

And what you are doing is setting the Image as the icon. This doesn't work because the setIcon() method requires objects which implement the Icon interface. Hope this helps.




回答3:


Try putting a foward slash before the package name in getResource() like so:

Image img = ImageIO.read(getClass().getResource("/Images/BBishopB.gif"));



回答4:


You can just find the image directly:

JButton jb = new JButton(new ImageIcon("pic.png")); //pic is in project root folder
//Tip: After placing the image in project folder, refresh the project in Eclipse.

OR if the image will be in a JAR, I usually create a function to do the retrieval for me so that I can re-use it.

public static ImageIcon retrieveIcon(String path){
    java.net.URL imgUrl = 'classpackage'.'classname'.class.getResource(path);
    ImageIcon icon = new ImageIcon(imgUrl);
    return icon;
}

Then I'll do,

JButton jb = new JButton(retrieveIcon("/pic.png"));



回答5:


Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));

This line tries to do too much all at one time which makes it difficult to track down an error when you get one. I suggest splitting it up:

URL imgURL = getClass().getResource("Images\\BBishopB.gif");
Image img = ImageIO.read(imgURL);

Now you can use the Eclipse debugger to check the return value of imgURL, which is the most likely candidate for the NPE. Even though this doesn't tell you why you get an error message, it narrows down the problem considerably.



来源:https://stackoverflow.com/questions/12691832/how-to-put-an-image-on-a-jbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!