0

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

Barett
  • 5,826
  • 6
  • 51
  • 55
Matthew Kemnetz
  • 845
  • 1
  • 15
  • 28
  • possible duplicate of [Add other components to JFrame with background](http://stackoverflow.com/questions/2960279/add-other-components-to-jframe-with-background) – Andrew Thompson Dec 04 '11 at 03:27

1 Answers1

1

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());
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • 1
    if you want to change the image later. Each Object has a `setBackgroundImage()` method, too. – Jon Egeland Dec 04 '11 at 03:25
  • 1
    `ImagePanel` is not part of the standard JDK, can you provide a link? – Jeffrey Dec 04 '11 at 03:26
  • @Jeffrey I was going to put the link but forgot where it was. Thanks. – Jon Egeland Dec 04 '11 at 03:26
  • @MatthewKemnetz you can specify the location using things like `../foo.png` for the above directory, or `images/foo.png` to go into an images folder to find the image. Google how to find paths to files. – Jon Egeland Dec 04 '11 at 03:28
  • [link] (http://www.wildcrest.com/Software/J2PrinterWorks/documentation/api/com/wildcrest/j2printerworks/ImagePanel.html) is the API for the class. I don't know where the actual library is, but `JPanel` works roughly the same. – Jon Egeland Dec 04 '11 at 03:31
  • 2
    -1 button.icon != button.background or in other words: when setting the icon, the text will _not_ be shown on top of it (as it would with a background image) – kleopatra Dec 04 '11 at 11:33
  • Which package contains the ImagePanel? – Queeg Jun 04 '23 at 17:51