|
It isn't always clear how to use GUI components. This example applet
displays one or more of each of the most common components, along
with just enough code to give a hint how to use them. To keep the
example simple, the default FlowLayoutManager is used.
For more information (and you will need more information!), look up these Objects in the back of Java In A Nutshell. Information about applets is in java.applet; information about these components is in java.awt; and information about Listeners is in java.awt.event. |
Note: This is a picture of the applet, not the applet itself.
The applet itself is not included because Java 1.1 is not supported
on Netscape browsers.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
// <applet code="Widgets.class" height=300 width=400> </applet>
public class Widgets extends Applet
{
// Declare components here to avoid visibility problems.
Label saySomething = new Label ("Let's use components!");
Button clickMe = new Button ("Click me!");
Checkbox onOrOff = new Checkbox ("Single Checkbox");
Choice suit = new Choice ();
List language = new List (3); // 3 rows visible
Scrollbar scrolly = new Scrollbar (Scrollbar.HORIZONTAL, 50, 10, 0, 110);
// orientation, value, bubblesize, min, max
TextField oneLine = new TextField ("This is a TextField.", 20);
TextArea manyLines = new TextArea ("TextArea\nOne\nTwo\nThree", 5, 20,
TextArea.SCROLLBARS_NONE);
CheckboxGroup direction = new CheckboxGroup ();
Checkbox north = new Checkbox ("North", true, direction);
Checkbox south = new Checkbox ("South", false, direction);
Checkbox east = new Checkbox ("East", false, direction);
Checkbox west = new Checkbox ("West", false, direction);
Button changeThings = new Button ("Change things");
public void init ()
{
/* This code is broken into three sections: creating and/or
modifying the widgets, laying out the widgets, and assigning
actions to the widgets. It might seem to make more sense to
organize the code widget by widget, but that turns out to make
layout much harder. */
// Create and/or modify widgets. Executable code (mostly "add")
// can't be done in the declarations, so it has to go here.
suit.add ("Clubs");
suit.add ("Diamonds");
suit.add ("Hearts");
suit.add ("Spades");
language.add ("English");
language.add ("Chinese");
language.add ("Japanese");
language.add ("German");
// Lay out widgets, using default FlowLayout manager.
add (saySomething);
add (clickMe);
add (onOrOff);
add (suit);
add (language);
add (scrolly);
add (oneLine);
add (manyLines);
add (changeThings);
add (north); add (south); add (east); add (west);
// Note: NOT add (direction);
// Assign actions to widgets.
// Button
clickMe.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event)
{
showStatus ("Button clicked.");
}
});
// Checkbox
onOrOff.addItemListener (new ItemListener () {
public void itemStateChanged (ItemEvent event)
{
showStatus ("Checkbox status is now " +
(event.getStateChange () == ItemEvent.SELECTED));
}
});
// Choice (drop-down list)
suit.addItemListener (new ItemListener () {
public void itemStateChanged (ItemEvent event)
{
showStatus ("Choice: " + suit.getSelectedItem ());
}
});
// List
language.addItemListener (new ItemListener () {
public void itemStateChanged (ItemEvent event)
{
showStatus ("List: " + language.getSelectedItem ());
}
});
// Scrollbar
scrolly.addAdjustmentListener (new AdjustmentListener () {
public void adjustmentValueChanged (AdjustmentEvent event)
{
showStatus ("Scrollbar: " + scrolly.getValue ());
}
});
// TextField
oneLine.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event)
{
showStatus ("TextField (ActionListener): " + oneLine.getText ());
}
});
oneLine.addTextListener (new TextListener () {
public void textValueChanged (TextEvent event)
{
showStatus ("TextField (TextListener): " + oneLine.getText ());
}
});
// TextArea
manyLines.addTextListener (new TextListener () {
public void textValueChanged (TextEvent event)
{
showStatus ("TextArea: " + manyLines.getText ());
}
});
// Button -- to demonstrate changing values programmatically
changeThings.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent event)
{
saySomething.setText ("I've been changed!");
oneLine.setText ("TextField");
manyLines.setText ("one\ntwo\nthree");
changeThings.setLabel ("More changes");
onOrOff.setLabel ("Destroy world");
onOrOff.setState (true);
suit.select ("Spades");
language.select (0);
scrolly.setValue (100 - scrolly.getValue ());
south.setState (true);
showStatus ("Changing a few things....");
}
});
// Checkboxs in a CheckboxGroup
MyGroupListener pickDirection = new MyGroupListener (); // see below
north.addItemListener (pickDirection);
south.addItemListener (pickDirection);
east.addItemListener (pickDirection);
west.addItemListener (pickDirection);
}
class MyGroupListener implements ItemListener
{
public void itemStateChanged (ItemEvent event)
{
Checkbox d = direction.getSelectedCheckbox ();
showStatus ("CheckboxGroup: " + d.getLabel ());
}
}
}