home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap17 / SimpleComponent / SimpleComponent.java < prev    next >
Encoding:
Java Source  |  1996-06-13  |  2.2 KB  |  94 lines

  1. // SimpleComponent - demonstrates how to attach simple
  2. //                   Abstract Window Toolkit components
  3. //                   to an applet
  4.  
  5. import java.applet.*;
  6. import java.awt.*;
  7.  
  8. public class SimpleComponent extends Applet
  9. {
  10.  
  11.     // Label - a label is a string of text that can be
  12.     //         modified by the program but not by
  13.     //         the user
  14.     private Label  m_label = new Label("Type in the text field");
  15.  
  16.     // TextField - a text field represents a one-line
  17.     //             optionally editable area of text
  18.     private TextField m_textField = new TextField(10);
  19.  
  20.     // Button - a button is a simple object that you can
  21.     //          select using the mouse or by tabbing to it
  22.     //          and pressing Enter
  23.     private Button m_button = new Button("Read Text Field");
  24.  
  25.     public SimpleComponent()
  26.     {
  27.         // TODO: Add Constructor code here
  28.     }
  29.  
  30.     public String getAppletInfo()
  31.     {
  32.         return "Name: SimpleComponent\r\n" +
  33.                "Author: Stephan R. Davis\r\n" +
  34.                "Created for Learn Java Now";
  35.     }
  36.  
  37.  
  38.     public void init()
  39.     {
  40.         resize(320, 240);
  41.  
  42.         // TODO: Place Addition Initialization code here
  43.         // Attach the label, text field, and button to the
  44.         // applet
  45.         add(m_label);
  46.         add(m_textField);
  47.         add(m_button);
  48.     }
  49.  
  50.     public void destroy()
  51.     {
  52.         // TODO: Place applet cleanup code here
  53.     }
  54.  
  55.     public void paint(Graphics g)
  56.     {
  57.     }
  58.  
  59.     public boolean action(Event event, Object obj)
  60.     {
  61.         // if this action sprung from a button...
  62.         Object oTarget = event.target;
  63.         if (oTarget instanceof Button)
  64.         {
  65.             // ...and if the button label is the same
  66.             // as "our button"
  67.             Button buttonTarget = (Button)oTarget;
  68.             String sButtonString = buttonTarget.getLabel();
  69.             if (sButtonString.compareTo("Read Text Field") == 0)
  70.             {
  71.                 String s = m_textField.getText();
  72.                 m_label.setText(s);
  73.                 return true;
  74.             }
  75.         }
  76.         return false;
  77.     }
  78.  
  79.     public void start()
  80.     {
  81.         // TODO: Place additional applet Start code here
  82.     }
  83.     
  84.     public void stop()
  85.     {
  86.     }
  87.  
  88.  
  89.  
  90.  
  91.     // TODO: Place additional applet Code here
  92.  
  93. }
  94.