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


SystemInfo

The applet in Listing 37.5 demonstrates how to build several “cards” of data into an applet. The file defines three classes:

  LabelField—A derivative of Panel that knows how to lay out its data and a label.
  SystemInfo—The applet class itself, which reads various system properties and displays them on the “cards” of a CardLayout.
  ButtonAction—An “inner class” of SystemInfo that responds to clicks on the applet’s two buttons.

Figure 37.9 shows the applet in action.

Listing 37.5 SystemInfo.java—This Applet Displays Several “Cards” of Information


import java.applet.Applet;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Label;
import java.awt.Button;
import java.awt.Font;
import java.awt.TextField;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class LabelField extends Panel {
  int fLabelWidth;
  Label fLabel;
  TextField fField;

  public LabelField(int theLabelWidth, String theLabel, String theValue) {
    this.fLabelWidth = theLabelWidth;
    add(this.fLabel = new Label(theLabel));
    add(this.fField = new TextField(theValue));
    fField.setEditable(false);
  }
  public void doLayout() {
    Dimension theDimension = getSize();
    Dimension theLabelSize = fLabel.getPreferredSize();
    Dimension theFieldSize = fField.getPreferredSize();
    fLabel.setBounds(0, 0, fLabelWidth, theLabelSize.height);
    fField.setBounds(fLabelWidth + 5, 0, theDimension.width - (fLabelWidth + 5),
      theFieldSize.height);
  }
}

public class SystemInfo extends Applet {
  CardLayout fCardLayout;
  Panel fPanel;
  Button fNextButton;
  Button fPrevButton;

  public void init() {
  Font theFont = new Font(“Helvetica”, Font.BOLD, 14);
  setLayout(new BorderLayout());
  add(“South”, fPanel = new Panel());

  fNextButton = new Button(“Next”);
  fPrevButton = new Button(“Previous”);
  ButtonAction aButtonAction = new ButtonAction();

  fNextButton.addActionListener(aButtonAction);
  fPrevButton.addActionListener(aButtonAction);

  fPanel.add(fPrevButton);
  fPanel.add(fNextButton);

add(“Center”, fPanel = new Panel());
  fPanel.setLayout(fCardLayout = new CardLayout());

  try {
    Panel aPanel = new Panel();
    aPanel.setLayout(new GridLayout(0, 1));
    aPanel.add(new Label(“System Properties”)).setFont(theFont);
    aPanel.add(new LabelField(100, “version:”,
      System.getProperty(“java.version”)));
    aPanel.add(new LabelField(100, “vendor:”,     System.getProperty(“java.vendor”)));
    aPanel.add(new LabelField(100, “vendor.url:”,
⇒System.getProperty(“java.vendor.url”)));
    fPanel.add(“system”, aPanel);

    aPanel = new Panel();
    aPanel.add(new Label(“Java Properties”)).setFont(theFont);
    aPanel.setLayout(new GridLayout(0, 1));
    aPanel.add(new LabelField(100, “class version:”,
      System.getProperty(“java.class.version”)));
    fPanel.add(“java”, aPanel);

    aPanel = new Panel();
    aPanel.setLayout(new GridLayout(0, 1));
    aPanel.add(new Label(“OS Properties”)).setFont(theFont);
    aPanel.add(new LabelField(100, “OS:”,
⇒System.getProperty(“os.name”)));
    aPanel.add(new LabelField(100, “OS Arch:”,
⇒System.getProperty(“os.arch”)));
    aPanel.add(new LabelField(100, “OS Version:”,
⇒System.getProperty(“os.version”)));
    fPanel.add(“os”, aPanel);

    aPanel = new Panel();
    aPanel.setLayout(new GridLayout(0, 1));
    aPanel.add(new Label(“Misc Properties”)).setFont(theFont);
    aPanel.add(new LabelField(100, “File Separator:”,   
System.getProperty(“file.separator”)));
    aPanel.add(new LabelField(100, “Path Separator:”,
      System.getProperty(“path.separator”)));
    aPanel.add(new LabelField(100, “Line Separator:”,
      System.getProperty(“line.separator”)));
    fPanel.add(“sep”, aPanel);
  } catch (SecurityException e) {
      System.out.println(“Security Exception: “ + e);
    }
  }

  class ButtonAction implements ActionListener {

    public void actionPerformed (ActionEvent theEvent) {
      Object anObject = theEvent.getSource();
      if (anObject == fNextButton)
        fCardLayout.next(fPanel);
      else if (anObject == fPrevButton)
        fCardLayout.previous(fPanel);
    }
  }
}


FIGURE 37.9  The first card of SystemInfo.

The Great Thread Race

Java enables your applet (or application) to have more than one thread of control. TRaceApplet, shown in Listing 37.6, starts several “racing” threads and shows them moving across the screen. You can use this applet to test your Web browser to see how well it shares time between threads of different priorities. (Most Web browsers do a poor job in this area—you shouldn’t rely upon relative priorities between threads for any critical feature.) Note, too, that this program has a main() method and can be run as an application as well as an applet.


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.