home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Graphics also supports a whole range of fill..., get..., and set... methods. Much of the time, however, you’re less interested in the image that appears on the screen than in the controls, such as Buttons and TextFields (which are implemented as Components). To add controls to your Container, you call the Container’s add() method. See line 27 of Listing 38.1:

add(fApplet);

Listing 38.3 shows a more elaborate version of Listing 38.1. In this program, you see more AWT components at work: a Button, a TextField, a Checkbox, a Choice, and several Labels.


You may have noticed that the AWT doesn’t include radio buttons—a group of buttons designed so that, at most, one selection is active. To make radio buttons, just build a CheckboxGroup—it’s documented in the JDK documentation.

Listing 38.3 HelloPlus.javaThis Version of the Application/Applet Has Some Working AWT Components


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class HelloPlus extends Applet {
  private Button fButton;
  private TextField fTextField;
  private Label fLabelForTextField;
  private Checkbox fCheckbox;
  private OKDialog fDialog;
  private Label fLabelForChoice;

  public static void main(String[] args) {
    HelloApplicationFrame theApplication = 
      new HelloApplicationFrame(“Hello Application”);
    theApplication.setSize(200,200);
    theApplication.show();
  }

  public void init() {
    add (new Label(“Hello, World!”));

    fTextField = new TextField(“TextField”);
    add(fTextField);
    fLabelForTextField = new Label(“Your text is TextField”);
    add(fLabelForTextField);

    setBackground(java.awt.Color.red);
    fButton = new Button(“White”);
    fButton.setBackground(java.awt.Color.white);
    add(fButton);

    fCheckbox = new Checkbox(“Checkbox”);
    add(fCheckbox);
    fDialog = OKDialog.makeDialog(“You clicked the checkbox!”);
    Choice theChoice = new Choice();
    theChoice.addItem(“Choice Item 1”);
    theChoice.addItem(“Choice Item 2”);
    theChoice.addItem(“Choice Item 3”);
    add(theChoice);
    fLabelForChoice = new Label(“You haven’t chosen anything”);
    add(fLabelForChoice);
    
    fButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        if (fButton.getLabel() == “White”) {
          setBackground(java.awt.Color.white);
          fButton.setLabel(“Red”);
        }
        else {
          setBackground(java.awt.Color.red);
          fButton.setLabel(“White”);
        }
        Component theComponents[] = getComponents();
        try {
          if (theComponents.length == 0)
            throw (new AWTException(“Cannot find the components”));
        } catch (AWTException theException) {
          System.err.println(“Exception: “ + theException.getMessage());
          theException.printStackTrace();
          System.exit(1);
        }
        for (short theIndex = 0; 
             theIndex < theComponents.length; theIndex++) 
               getComponent(theIndex).setBackground(getBackground());
        if (fButton.getLabel() == “White”)
          fButton.setBackground(java.awt.Color.white);
        else
          fButton.setBackground(java.awt.Color.red);
      }
    });

     fTextField.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
          fLabelForTextField.setText(“Your text is “ + 
                                   fTextField.getText());
       }
     });

     fCheckbox.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent e) {
         if (fCheckbox.getState())  // the box is checked
           fDialog.show();
         else
           fDialog.setVisible(false);
       }
     });

     theChoice.addItemListener(new ItemListener() {
       public void itemStateChanged(ItemEvent e) {
         try {
           Object theSelectedItem[] = 
             e.getItemSelectable().getSelectedObjects();
           if (theSelectedItem.length != 1)
             throw(
               new AWTException(
                 “Number of selected items in choice is “ + 
                                  theSelectedItem.length));
           fLabelForChoice.setText(“Your choice is “ + 
             theSelectedItem[0]);
        } catch (AWTException theException) {
          System.err.println(“Exception: “ + theException.getMessage());
          theException.printStackTrace();
          System.exit(1);
        } // end catch
      } // end method
     }); // end addItemListener
  } // end init

} // end HelloPlus

class HelloApplicationFrame extends Frame {
  private HelloPlus fApplet;

  public HelloApplicationFrame(String name) {
    super(name);
    addWindowListener(new HelloWindowAdapter());
    fApplet = new HelloPlus();
    fApplet.init();
    fApplet.start();
    add(fApplet);
  }

  // We’re still within HelloApplicationFrame;
  // these adapters are inner classes
  class HelloWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      fApplet.stop();
      fApplet.destroy();
      System.exit(0);
    }
  } // end inner class HelloWindowAdapter
} // end HelloApplicationFrame

class OKDialog extends Dialog {
  private Button fOKButton;
  static private Frame fFrame;

  private OKDialog(Frame theParent, String theMessage) {
    super(theParent, true); // call Dialog’s modal constructor
    fOKButton = new Button(“OK”);
    add(fOKButton, BorderLayout.CENTER );
    Label theMessageLabel = new Label(theMessage);
    add (theMessageLabel, BorderLayout.NORTH );
    pack();

    fOKButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        setVisible( false );
      }
    }
    );

  } // end constructor

  static public OKDialog makeDialog(String theMessage) {
    if (fFrame == null)
      fFrame = new Frame();
    OKDialog theResult = new OKDialog(fFrame, theMessage);
    fFrame.setSize(theResult.getSize().width,
                   theResult.getSize().height);
    return theResult;
  }
} // end OKDialog class

If you compile HelloPlus.java and run it as an application or as an applet, your screen will resemble Figure 38.5. This section describes how the code in HelloPlus.java works.


FIGURE 38.5  By default, HelloPlus fills a 200 × 200 pixel panel.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.