home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / ComponentDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.7 KB  |  104 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.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20.  
  21. public class ComponentDemo extends Applet
  22.                implements ComponentListener {
  23.     TextArea display;
  24.     Frame someFrame;
  25.  
  26.     public void init() {
  27.     someFrame = new SomeFrame(this);
  28.  
  29.     setLayout(new BorderLayout());
  30.  
  31.     display = new TextArea(5, 20);
  32.     display.setEditable(false);
  33.     add("Center", display);
  34.     }
  35.  
  36.     public void stop() {
  37.     someFrame.setVisible(false);
  38.     }
  39.  
  40.     public void start() {
  41.     someFrame.setVisible(true);
  42.     }
  43.  
  44.     protected void displayMessage(String message) {
  45.     try {
  46.         display.append(message + "\n");
  47.     } catch (Exception e) {
  48.     }
  49.  
  50.     System.out.println(message);
  51.     }
  52.  
  53.     public void componentHidden(ComponentEvent e) {
  54.     displayMessage("componentHidden event from "
  55.                + e.getComponent().getClass().getName());
  56.     }
  57.  
  58.     public void componentMoved(ComponentEvent e) {
  59.     displayMessage("componentMoved event from "
  60.                + e.getComponent().getClass().getName());
  61.     }
  62.  
  63.     public void componentResized(ComponentEvent e) {
  64.     displayMessage("componentResized event from "
  65.                + e.getComponent().getClass().getName());
  66.     }
  67.  
  68.     public void componentShown(ComponentEvent e) {
  69.     displayMessage("componentShown event from "
  70.                + e.getComponent().getClass().getName());
  71.     }
  72. }
  73.  
  74. class SomeFrame extends Frame 
  75.         implements ItemListener {
  76.     Label label;
  77.     Checkbox checkbox;
  78.  
  79.     SomeFrame(ComponentListener listener) {
  80.     super("SomeFrame");
  81.  
  82.     label = new Label("This is a Label", Label.CENTER);
  83.     add("Center", label);
  84.  
  85.     checkbox = new Checkbox("Label visible", true);
  86.     checkbox.addItemListener(this);
  87.     add("South", checkbox);
  88.  
  89.     label.addComponentListener(listener);
  90.     checkbox.addComponentListener(listener);
  91.     this.addComponentListener(listener);
  92.  
  93.     pack();
  94.     }
  95.  
  96.     public void itemStateChanged(ItemEvent e) {
  97.     if (e.getStateChange() == ItemEvent.SELECTED) {
  98.         label.setVisible(true);
  99.     } else {
  100.         label.setVisible(false);
  101.     }
  102.     }
  103. }
  104.