Source of WindowListenerDemo.java


  1: 
  2: import javax.swing.JButton;
  3: import javax.swing.JFrame;
  4: import java.awt.Color;
  5: import java.awt.Container;
  6: import java.awt.FlowLayout;
  7: import java.awt.event.ActionEvent;
  8: import java.awt.event.ActionListener;
  9: import java.awt.event.WindowEvent;
 10: import java.awt.event.WindowListener;
 11: 
 12: public class WindowListenerDemo extends JFrame
 13:                    implements ActionListener, WindowListener
 14: {
 15:     public static final int WIDTH = 300;
 16:     public static final int HEIGHT = 200;
 17: 
 18:     public WindowListenerDemo( )
 19:     {
 20:         setSize(WIDTH, HEIGHT);
 21: 
 22:         addWindowListener(this);
 23:         setTitle("Window Listener Demonstration");
 24:         Container content = getContentPane( );
 25:         content.setBackground(Color.BLUE);
 26: 
 27:         content.setLayout(new FlowLayout( ));
 28: 
 29:         JButton stopButton = new JButton("Red");
 30:         stopButton.addActionListener(this);
 31:         content.add(stopButton);
 32: 
 33:         JButton goButton = new JButton("Green");
 34:         goButton.addActionListener(this);
 35:         content.add(goButton);
 36:     }
 37: 
 38:     public void actionPerformed(ActionEvent e)
 39:     {
 40:         Container content = getContentPane( );
 41:         if (e.getActionCommand( ).equals("Red"))
 42:             content.setBackground(Color.RED);
 43:         else if (e.getActionCommand( ).equals("Green"))
 44:             content.setBackground(Color.GREEN);
 45:         else
 46:             System.out.println("Error in WindowListenerDemo.");
 47:     }
 48: 
 49:     public void windowClosing(WindowEvent e)
 50:     {
 51:         this.dispose( );
 52:         System.exit(0);
 53:     }
 54: 
 55:     public void windowOpened(WindowEvent e)
 56:     {}
 57: 
 58:     public void windowClosed(WindowEvent e)
 59:     {}
 60: 
 61:     public void windowIconified(WindowEvent e)
 62:     {}
 63: 
 64:     public void windowDeiconified(WindowEvent e)
 65:     {}
 66: 
 67:     public void windowActivated(WindowEvent e)
 68:     {}
 69: 
 70:     public void windowDeactivated(WindowEvent e)
 71:     {}
 72: 
 73:     public static void main(String[] args)
 74:     {
 75:         WindowListenerDemo demoWindow = new WindowListenerDemo( );
 76:         demoWindow.setVisible(true);
 77:     }
 78: }