import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SampleButton { // set up GUI public static void main(String args[]) { JFrame application = new JFrame("Button Demonstrator"); application.setLayout(new FlowLayout()); // create buttons JButton button = new JButton("Plain Button"); application.add(button); button.addActionListener(new ButtonHandler()); Icon bug1 = new ImageIcon("bug1.png"); Icon bug2 = new ImageIcon("bug2.png"); button = new JButton("Fancy Button", bug1); button.setRolloverIcon(bug2); application.add(button); button.addActionListener(new ButtonHandler()); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.setSize(275, 200); application.setVisible(true); } } class ButtonHandler implements ActionListener { // handle button event public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(null, "You pressed: " + event.getActionCommand()); } } /******************************************************************************* * Based on example code from Deitel & Associates, Inc. Prentice Hall. (2002) *******************************************************************************/