I want to add images to my JButtons. I have tried to add the rollover icon command to one of my buttons, but I keep getting an exception even though eclipse doesn't say that there are any errors. I have saved the images in workspace/projectname/src where the class files are, and they are called a and b, and they are JPEG images.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class button extends JFrame {
private static final long serialVersionUID = 1L;
private JButton hi;
private JButton custom;
public button() {
super("The title");
setLayout(new FlowLayout());
hi = new JButton("Hi button");
Icon a = new ImageIcon(getClass().getResource("a.JPEG"));
Icon b = new ImageIcon(getClass().getResource("b.JPEG"));
custom = new JButton("custom", a);
custom.setRolloverIcon(b);
add(custom);
add(hi);
HandlerClass handler = new HandlerClass();
hi.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}
import javax.swing.JFrame;
public class buttonm {
public static void main(String[] args) {
button hello = new button();
hello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hello.setSize(350,100);
hello.setVisible(true);
}
}