I use the code below to add a background image to a JPanel
, problem is I can't figure out a way to add an image to a JButton
.. any ideas?
public void displayGUI() {
JFrame frame = new JFrame("Painting Example");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(440, 385);
JPanel panel = new JPanel();
frame.add(panel);
JButton button = new JButton("want picture here");
panel.add(button);
button.addActionListener(new Action7());
}
class Custom4 extends JPanel {
public BufferedImage image;
public Custom4() {
try {
image = ImageIO.read(new URL("http://i68.tinypic.com/2itmno6.jpg"));
} catch (IOException ioe) {
System.out.println("Unable to fetch image.");
ioe.printStackTrace();
}
}
public Dimension getPreferredSize() {
return (new Dimension(image.getWidth(), image.getHeight()));
}
public void paintComponent(Graphics x) {
super.paintComponent(x);
x.drawImage(image, 0, 0, this);
}
}