Source of ButtonIcons.java


  1: import javax.swing.*;
  2: import java.awt.*;
  3: import java.awt.event.*;
  4: 
  5: public class ButtonIcons extends JFrame
  6:         implements ActionListener
  7: {
  8:         public static final int WIDTH = 400;
  9:         public static final int HEIGHT = 300;
 10: 
 11:         private Container contentPane;
 12:         private JPanel buttonPanel;
 13: 
 14:         public ButtonIcons()
 15:         {
 16:                 setSize(WIDTH, HEIGHT);
 17:                 addWindowListener(new WindowDestroyer());
 18:                 setTitle("Button Icon Demo");
 19:                 contentPane = getContentPane();
 20:                 contentPane.setBackground(Color.GRAY);
 21:                 contentPane.setLayout(new BorderLayout());
 22: 
 23:                 buttonPanel = new JPanel();
 24:                 buttonPanel.setLayout(new FlowLayout());
 25:                 buttonPanel.setBackground(Color.GRAY);
 26: 
 27:                 JButton redButton = new JButton("Red");
 28:                 redButton.addActionListener(this);
 29:                 buttonPanel.add(redButton);
 30: 
 31:                 JButton blueButton = new JButton("Blue");
 32:                 blueButton.addActionListener(this);
 33:                 buttonPanel.add(blueButton);
 34: 
 35:                 JButton greenButton = new JButton("Green");
 36:                 greenButton.addActionListener(this);
 37:                 buttonPanel.add(greenButton);
 38: 
 39:                 contentPane.add(buttonPanel, BorderLayout.NORTH);
 40:         }
 41: 
 42:         public void actionPerformed(ActionEvent e)
 43:         {
 44:                 if(e.getActionCommand().equals("Red"))
 45:                 {
 46:                         contentPane.setBackground(Color.RED);
 47:                         buttonPanel.setBackground(Color.RED);
 48:                 }
 49:                 else if(e.getActionCommand().equals("Blue"))
 50:                 {
 51:                         contentPane.setBackground(Color.BLUE);
 52:                         buttonPanel.setBackground(Color.BLUE);
 53:                 }
 54:                 else if(e.getActionCommand().equals("Green"))
 55:                 {
 56:                         contentPane.setBackground(Color.GREEN);
 57:                         buttonPanel.setBackground(Color.GREEN);
 58:                 }
 59:         }
 60: 
 61:         public static void main(String[] args)
 62:         {
 63:                 ButtonIcons gui = new ButtonIcons();
 64:                 gui.setVisible(true);
 65:         }
 66: }