// Add icons to a button. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonIcons implements ActionListener { JLabel jlab; JButton jbtnFirst; JButton jbtnSecond; ButtonIcons() { ImageIcon myIcon = new ImageIcon("myIcon.gif"); ImageIcon myDisIcon = new ImageIcon("myDisIcon.gif"); ImageIcon myROIcon = new ImageIcon("myROIcon.gif"); ImageIcon myPIcon = new ImageIcon("myPIcon.gif"); // Create a new JFrame container. JFrame jfrm = new JFrame("Use Button Icons"); // Specify FlowLayout for the layout manager. jfrm.getContentPane().setLayout(new FlowLayout()); // Give the frame an initial size. jfrm.setSize(220, 100); // Terminate the program when the user closes the application. jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a text-based label. jlab = new JLabel("Press a button."); // Make two buttons. jbtnFirst = new JButton("First", myIcon); jbtnSecond = new JButton("Second", myIcon); // Set rollover icons. jbtnFirst.setRolloverIcon(myROIcon); jbtnSecond.setRolloverIcon(myROIcon); // Set pressed icons. jbtnFirst.setPressedIcon(myPIcon); jbtnSecond.setPressedIcon(myPIcon); // Set disabled icons. jbtnFirst.setDisabledIcon(myDisIcon); jbtnSecond.setDisabledIcon(myDisIcon); // Add action listeners. jbtnFirst.addActionListener(this); jbtnSecond.addActionListener(this); // Add the buttons to the content pane. jfrm.getContentPane().add(jbtnFirst); jfrm.getContentPane().add(jbtnSecond); // Add the label to the frame. jfrm.getContentPane().add(jlab); // Display the frame. jfrm.setVisible(true); } // Handle button events. public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand().equals("First")) { jlab.setText("First button was pressed."); if(jbtnSecond.isEnabled()) { jlab.setText("Second button is disabled."); jbtnSecond.setEnabled(false); } else { jlab.setText("Second button is enabled."); jbtnSecond.setEnabled(true); } } else jlab.setText("Second button was pressed. "); } public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { new ButtonIcons(); } }); } }