home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap17 / SimpleComponent3 / SimpleComponent.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  2.4 KB  |  58 lines

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Button;
  4. import java.awt.Container;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Label;
  8. import java.awt.Panel;
  9. import java.awt.TextField;
  10.  
  11. public class SimpleComponent extends Applet {
  12.    private Label m_label = new Label("Type in the text field");
  13.    private TextField m_textField = new TextField(10);
  14.    private Button m_button = new Button("Read Text Field");
  15.  
  16.    public void start() {
  17.    }
  18.  
  19.    public void stop() {
  20.    }
  21.  
  22.    public String getAppletInfo() {
  23.       return "Name: SimpleComponent\r\n" + "Author: Stephan R. Davis\r\n" + "Created for Learn Java Now";
  24.    }
  25.  
  26.    public boolean action(Event event, Object obj) {
  27.       Object oTarget = event.target;
  28.       if (oTarget instanceof Button) {
  29.          Button buttonTarget = (Button)oTarget;
  30.          String sButtonString = buttonTarget.getLabel();
  31.          if (sButtonString.compareTo("Read Text Field") == 0) {
  32.             String s = this.m_textField.getText();
  33.             this.m_label.setText(s);
  34.             return true;
  35.          }
  36.       }
  37.  
  38.       return false;
  39.    }
  40.  
  41.    public void destroy() {
  42.    }
  43.  
  44.    public void init() {
  45.       ((Applet)this).resize(320, 240);
  46.       ((Container)this).setLayout(new BorderLayout());
  47.       Panel panelText = new Panel();
  48.       ((Container)panelText).setLayout(new BorderLayout());
  49.       ((Container)panelText).add("North", this.m_label);
  50.       ((Container)panelText).add("Center", this.m_textField);
  51.       ((Container)this).add("Center", panelText);
  52.       ((Container)this).add("East", this.m_button);
  53.    }
  54.  
  55.    public void paint(Graphics g) {
  56.    }
  57. }
  58.