home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / ButtonDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  92 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import com.sun.java.swing.AbstractButton;
  20. import com.sun.java.swing.JButton;
  21. import com.sun.java.swing.JPanel;
  22. import com.sun.java.swing.JFrame;
  23. import com.sun.java.swing.ImageGlyph;
  24.  
  25. public class ButtonDemo extends JPanel
  26.                         implements java.awt.event.ActionListener {
  27.  
  28. JButton b1, b2, b3;
  29.  
  30.     public ButtonDemo() {
  31.     super(true);     // true = please double buffer
  32.     ImageGlyph leftButtonGlyph = new ImageGlyph("right.gif");
  33.     ImageGlyph middleButtonGlyph = new ImageGlyph("middle.gif");
  34.     ImageGlyph rightButtonGlyph = new ImageGlyph("left.gif");
  35.  
  36.         b1 = new JButton("Disable middle button", leftButtonGlyph);
  37.     b1.setVerticalTextPosition(AbstractButton.CENTER);
  38.     b1.setHorizontalTextPosition(AbstractButton.LEFT);
  39.     b1.setKeyAccelerator('d');
  40.     b1.setActionCommand("disable");
  41.  
  42.         b2 = new JButton("Middle button", middleButtonGlyph);
  43.     b2.setVerticalTextPosition(AbstractButton.BOTTOM);
  44.     b2.setHorizontalTextPosition(AbstractButton.CENTER);
  45.     b2.setKeyAccelerator('m');
  46.  
  47.         b3 = new JButton("Enable middle button", rightButtonGlyph);
  48.     //Use the default text position of CENTER, RIGHT.
  49.     b3.setKeyAccelerator('e');
  50.     b3.setActionCommand("enable");
  51.         b3.setEnabled(false);
  52.  
  53.     //Listen for actions on buttons 1 and 3.
  54.     b1.addActionListener(this);
  55.     b3.addActionListener(this);
  56.  
  57.         //Add Components to the JPanel, using the default FlowLayout. 
  58.         add(b1);
  59.         add(b2);
  60.         add(b3);
  61.     }
  62.  
  63.     public void actionPerformed(java.awt.event.ActionEvent e) {
  64.         if (e.getActionCommand().equals("disable")) {
  65.             b2.setEnabled(false);
  66.             b1.setEnabled(false);
  67.             b3.setEnabled(true);
  68.         } else { 
  69.             b2.setEnabled(true);
  70.             b1.setEnabled(true);
  71.             b3.setEnabled(false);
  72.         }
  73.     }
  74.     
  75.     public static void main(String[] args) {
  76.     /*
  77.      * Create a window.  Use JFrame since this window will include 
  78.      * lightweight components.
  79.      */
  80.     JFrame frame = new JFrame("ButtonDemo");
  81.  
  82.     WindowListener l = new WindowAdapter() {
  83.         public void windowClosing(WindowEvent e) {System.exit(0);}
  84.     };
  85.     frame.addWindowListener(l);
  86.  
  87.     frame.add("Center", new ButtonDemo());
  88.     frame.pack();
  89.     frame.show();
  90.     }
  91. }
  92.