Java JButton set mouse rollover icon image

Description

Java JButton set mouse rollover icon image

import java.awt.FlowLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    Icon icon = new ImageIcon(getClass().getResource("icon.png"));
    Icon icon2 = new ImageIcon(getClass().getResource("icon2.png"));
    /*from   w w  w.  jav  a 2s .com*/
    JButton closeButton1 = new JButton("Close", icon);
    closeButton1.setRolloverIcon(icon2); // set rollover image
    
    getContentPane().add(closeButton1);
  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related