Source of IconDemo.java


  1: 
  2: import javax.swing.*;
  3: import java.awt.*; 
  4: import java.awt.event.*;
  5: 
  6: /**
  7:  Simple demonstration of putting icons in buttons and labels.
  8: */
  9: public class IconDemo extends JFrame implements ActionListener
 10: {
 11:     public static final int WIDTH = 400;
 12:     public static final int HEIGHT = 200;
 13: 
 14:     private JTextField message;
 15: 
 16:     public IconDemo( )
 17:     {
 18:         setSize(WIDTH, HEIGHT);
 19:         addWindowListener(new WindowDestroyer( ));
 20:         setTitle("Icon Demonstration");
 21:         Container content = getContentPane( );
 22:         content.setBackground(Color.WHITE);
 23:         content.setLayout(new BorderLayout( ));
 24: 
 25:         JLabel niceLabel = new JLabel("Nice day!");
 26:         ImageIcon smileyIcon = new ImageIcon("smiley.gif");
 27:         niceLabel.setIcon(smileyIcon);
 28:         content.add(niceLabel, BorderLayout.NORTH);
 29: 
 30:         JPanel buttonPanel = new JPanel( );
 31:         buttonPanel.setLayout(new FlowLayout( ));
 32:         JButton helloButton = new JButton("Hello");
 33:         ImageIcon dukeWavingIcon = new ImageIcon("duke_waving.gif");
 34:         helloButton.setIcon(dukeWavingIcon);
 35:         helloButton.addActionListener(this);
 36:         buttonPanel.add(helloButton);
 37:         JButton byeButton = new JButton("Good bye");
 38:         ImageIcon dukeStandingIcon =
 39:                   new ImageIcon("duke_standing.gif");
 40:         byeButton.setIcon(dukeStandingIcon);
 41:         byeButton.addActionListener(this);
 42:         buttonPanel.add(byeButton);
 43:         content.add(buttonPanel, BorderLayout.SOUTH);
 44: 
 45:         message = new JTextField(30);
 46:         content.add(message, BorderLayout.CENTER);
 47:     }
 48: 
 49:     public void actionPerformed(ActionEvent e)
 50:     {
 51:        if (e.getActionCommand( ).equals("Hello"))
 52:            message.setText("Glad to meet you!");
 53:        else if (e.getActionCommand( ).equals("Good bye"))
 54:            message.setText(
 55:                   "OK, click the upper right button. I'll miss you.");
 56:        else
 57:            System.out.println("Error in button interface.");
 58:     }
 59: 
 60:     /**
 61:      Creates and displays a window of the class IconDemo.
 62:     */
 63:     public static void main(String[] args)
 64:     {
 65:         IconDemo iconGui = new IconDemo( );
 66:         iconGui.setVisible(true);
 67:     }
 68: }