Search code examples
javaswingjpaneljlabelimageicon

Using an image for the background of a JPanel and JButton


I am trying to use an image I made in photoshop as the background for my GUI. How do I do that? also I made some images I want to display in the button backgrounds after the action is performed...


Solution

  • For the JButton, use this:

    JButton button = new JButton("Button Name", new ImageIcon("foo.png");
    

    The Panel is a bit more interesting. This is a good method, though:

    ImagePanel panel = new ImagePanel(new ImageIcon("foo.png").getImage());