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

  1. // SimpleComponent - demonstrates how to attach simple
  2. //                   Abstract Window Toolkit components
  3. //                   to an applet using a Border Layout
  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.         // Use a BorderLayout manager
  44.         setLayout(new BorderLayout());
  45.  
  46.         // create a panel with the label and text together
  47.         // (put the label above the text field - using Center
  48.         // for the text field gives it any remaining room
  49.         // after the label takes its cut)
  50.         Panel panelText = new Panel();
  51.         panelText.setLayout(new BorderLayout());
  52.         panelText.add("North", m_label);
  53.         panelText.add("Center", m_textField);
  54.  
  55.         // now add the panel to the applet on the left and
  56.         // the button on the right
  57.         add("Center", panelText);
  58.         add("East", m_button);
  59.     }
  60.  
  61.     public void destroy()
  62.     {
  63.         // TODO: Place applet cleanup code here
  64.     }
  65.  
  66.     public void paint(Graphics g)
  67.     {
  68.     }
  69.  
  70.     public boolean action(Event event, Object obj)
  71.     {
  72.         // if this action sprung from a button...
  73.         Object oTarget = event.target;
  74.         if (oTarget instanceof Button)
  75.         {
  76.             // ...and if the button label is the same
  77.             // as "our button"
  78.             Button buttonTarget = (Button)oTarget;
  79.             String sButtonString = buttonTarget.getLabel();
  80.             if (sButtonString.compareTo("Read Text Field") == 0)
  81.             {
  82.                 String s = m_textField.getText();
  83.                 m_label.setText(s);
  84.                 return true;
  85.             }
  86.         }
  87.         return false;
  88.     }
  89.  
  90.     public void start()
  91.     {
  92.         // TODO: Place additional applet Start code here
  93.     }
  94.     
  95.     public void stop()
  96.     {
  97.     }
  98.  
  99.  
  100.  
  101.  
  102.     // TODO: Place additional applet Code here
  103.  
  104. }
  105.