import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* Demonstrates the basic use of each of the various Components ("widgets")
* that can be used in an Applet. This code is broken into three sections:
*
* - Constructing the widgets. To enable access from other classes
* (including inner classes), the Components should be declared
* as instance variables of this class, though they may be
* defined (constructed) in the init() method, if desired.
* - Inside init(), layout is done by adding the Components to the
* appropriate Containers.
* - Also inside init(), the listeners are added to each component.
*
* It might seem to make more sense to organize the code widget by widget,
* but it turns out to be much easier to design and fiddle with the layout
* if all the add() messages are together in one place.
*
* @author David Matuszek
* @version November 11, 2001
*/
public class Widgets extends Applet {
// Declare components here to avoid access problems.
Label label = new Label("This is a Label");
Button button = new Button("Button");
Button changeButton = new Button("Change things");
Checkbox checkbox = new Checkbox("Single Checkbox");
Choice choice = new Choice();
List list = new List(3); // 3 rows visible
// new Scrollbar(orientation, value, bubblesize, min, max)
Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110);
TextField textField = new TextField("This is a TextField.", 20);
TextArea textArea = new TextArea("This is a TextArea.\nIt's about 20 n's wide:\n" +
"nnnnnnnnnnnnnnnnnnnn\n" +
"and 5 lines tall.\n" +
"Here are 20 iiiiiiiiiiiiiiiiiiii's", 5, 20,
TextArea.SCROLLBARS_NONE);
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("Checkbox 1", true, checkboxGroup);
Checkbox checkbox2 = new Checkbox("Checkbox 2", false, checkboxGroup);
Checkbox checkbox3 = new Checkbox("Checkbox 3", false, checkboxGroup);
Checkbox checkbox4 = new Checkbox("Checkbox 4", false, checkboxGroup);
/**
* Creates the GUI.
*/
public void init() {
// Lay out the widgets.
// Add choices (Strings) to the Choice component
choice.add("choice 1");
choice.add("choice 2");
choice.add("choice 3");
choice.add("choice 4");
// Add list elements (Strings) to the List component
list.add("list 1");
list.add("list 2");
list.add("list 3");
list.add("list 4");
list.add("list 5");
// Add remaining components, including choice and list, to the Applet
add(label);
add(button);
add(checkbox);
add(choice);
add(scrollbar);
add(list);
add(textField);
add(textArea);
// Add individual checkboxes, NOT add (checkboxGroup);
add(checkbox1);
add(checkbox2);
add(checkbox3);
add(checkbox4);
// The changeButton modifies a number of things in the GUI
add(changeButton);
changeButton.setBackground(Color.orange);
// Assign listeners to widgets.
// Button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showStatus("Button clicked.");
}
});
// Checkbox
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
showStatus("Checkbox status is now " +
(event.getStateChange() == ItemEvent.SELECTED));
}
});
// Choice (drop-down list)
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
showStatus("Choice: " + choice.getSelectedItem());
}
});
// List
list.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
showStatus("List: " + list.getSelectedItem());
}
});
// Scrollbar
scrollbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent event) {
showStatus("Scrollbar: " + scrollbar.getValue());
}
});
// TextField
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showStatus("TextField(ActionListener): " + textField.getText());
}
});
textField.addTextListener(new TextListener() {
public void textValueChanged(TextEvent event) {
showStatus("TextField(TextListener): " + textField.getText());
}
});
// TextArea
textArea.addTextListener(new TextListener() {
public void textValueChanged(TextEvent event) {
showStatus("TextArea: " + textArea.getText());
}
});
// Button -- to demonstrate changing values programmatically
changeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
label.setText("Changed label");
textField.setText("This text is changed");
textArea.setText("The text in this TextArea\nhas been changed");
changeButton.setLabel("Already changed.");
changeButton.setEnabled(false);
checkbox.setLabel("Destroy world");
checkbox.setState(true);
choice.select("choice 3");
list.select(0);
scrollbar.setValue(100 - scrollbar.getValue());
checkbox3.setState(true);
}
});
// Checkboxes in a CheckboxGroup
MyGroupListener myGroupListener = new MyGroupListener(); // see below
checkbox1.addItemListener(myGroupListener);
checkbox2.addItemListener(myGroupListener);
checkbox3.addItemListener(myGroupListener);
checkbox4.addItemListener(myGroupListener);
// set initial applet size (appletviewer only)
setSize(485, 200);
} // end init()
/**
* Listener for the checkboxGroup. Implemented as a member class instead
* of an anonymous inner class because it is used several times.
*/
class MyGroupListener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
Checkbox d = checkboxGroup.getSelectedCheckbox();
showStatus("CheckboxGroup: " + d.getLabel());
}
}
}