home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch08 / Action1.java next >
Text File  |  1998-12-14  |  2KB  |  82 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class Action1 extends JPanel implements ActionListener
  7. {
  8.    public Action1()
  9.    {
  10.         JButton button1;
  11.         JRadioButton radioButton1;
  12.         JRadioButton radioButton2;
  13.         ButtonGroup grp;
  14.  
  15.  
  16.   //Buffer to reduce flicker
  17.         setDoubleBuffered(true);
  18.  
  19.         button1 = new JButton("Button");
  20.         button1.addActionListener(this);
  21.         button1.setActionCommand("Button Activated");
  22.         add(button1); 
  23.  
  24.         grp = new ButtonGroup();
  25.  
  26.         radioButton1 = new JRadioButton("One");
  27.         radioButton1.addActionListener(this);
  28.         radioButton1.setActionCommand("One Activated");
  29.         grp.add(radioButton1);
  30.         add(radioButton1);
  31.  
  32.         radioButton2 = new JRadioButton("Two");
  33.         radioButton2.addActionListener(this);
  34.         radioButton2.setActionCommand("Two Activated");
  35.         grp.add(radioButton2);
  36.         add(radioButton2);
  37.  
  38.    }//constructor
  39.  
  40.    public void actionPerformed(ActionEvent e)
  41.    {
  42.         String cmd;
  43.         Object source;
  44.  
  45.         source = e.getSource();
  46.         cmd = e.getActionCommand();
  47.  
  48.  
  49.         System.out.println("Action: "+cmd+"\n\tperformed by: "+source);
  50.         System.out.println();
  51.  
  52.    }//actionPerformed
  53.  
  54.    public static void main(String s[])
  55.     {
  56.         JFrame f = new JFrame("Action1 ");
  57.         Action1 panel = new Action1();
  58.  
  59. f.getContentPane().add(panel, "Center");
  60.  
  61.         f.setSize(200,100);
  62.         f.setVisible(true);
  63.         f.addWindowListener(new WindowCloser());
  64.  
  65.  
  66.     }//main
  67.  
  68. }//class Actions
  69.  
  70. class WindowCloser extends WindowAdapter
  71. {
  72.     public void windowClosing(WindowEvent e)
  73.     {
  74.         Window win = e.getWindow();
  75.         win.setVisible(false);
  76.         win.dispose();
  77.         System.exit(0);
  78.     }//windowClosing
  79. }//class WindowCloser
  80.  
  81.  
  82.