Image Rollover not working
Can anyone help me with the code please. I have two images 1.jpg
and 2.jpg
, when i run the program ,1.jpg
appears on button, but when i hover the mouse on button, 2.jpg
does not appear. Below is the code, Thanks
import javax.swing.*;
class ButtonRollover {
public static void main(String[] args) throws Exception {
String path1 = ("C:\\1.jpg");
String path2 = ("C:\\2.jpg");
final JLabel pic1 = new JLabel(new ImageIcon(path1));
final JLabel pic2 = new JLabel(new ImageIcon(path2));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JButton button = new JButton("Hover");
button.setRollover开发者_StackOverflow中文版Icon(new ImageIcon("C:\\2.jpg"));
button.add(pic1);
button.setRolloverEnabled(true);
JOptionPane.showMessageDialog(null, button);
}
});
}
}
You're not supposed to add a label inside a button. Just set its icon with
button.setIcon(new ImageIcon(path1));
instead of
button.add(pic1);
精彩评论