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: *
    *
  1. 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.
  2. *
  3. Inside init(), layout is done by adding the Components to the * appropriate Containers.
  4. *
  5. Also inside init(), the listeners are added to each component.
  6. *
* 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. *

* This code is the same as the earlier Widgets class, except that the * anonymous inner classes have been replaced by member classes. * * @author David Matuszek * @version November 11, 2001 */ public class Widgets2 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 MyButtonListener()); // Checkbox checkbox.addItemListener(new MyCheckboxListener()); // Choice(drop-down list) choice.addItemListener(new MyChoiceListener()); // List list.addItemListener(new MyListListener()); // Scrollbar scrollbar.addAdjustmentListener(new MyScrollbarListener()); // TextField textField.addActionListener(new MyTextFieldListeners()); textField.addTextListener(new MyTextFieldListeners()); // TextArea textArea.addTextListener(new MyTextAreaListener()); // Button -- to demonstrate changing values programmatically changeButton.addActionListener(new MyChangeButtonListener()); // Checkboxes in a CheckboxGroup MyCheckboxGroupListener myCheckboxGroupListener = new MyCheckboxGroupListener(); checkbox1.addItemListener(myCheckboxGroupListener); checkbox2.addItemListener(myCheckboxGroupListener); checkbox3.addItemListener(myCheckboxGroupListener); checkbox4.addItemListener(myCheckboxGroupListener); // set initial applet size(appletviewer only) setSize(485, 200); } // end init() //-------------------------- Inner classes -------------------------// // Button listener class MyButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { showStatus("Button clicked."); } } // Checkbox listener class MyCheckboxListener implements ItemListener { public void itemStateChanged(ItemEvent event) { showStatus("Checkbox status is now " + (event.getStateChange() == ItemEvent.SELECTED)); } }; // Choice (drop-down list) listener class MyChoiceListener implements ItemListener { public void itemStateChanged(ItemEvent event) { showStatus("Choice: " + choice.getSelectedItem()); } }; // List listener class MyListListener implements ItemListener { public void itemStateChanged(ItemEvent event) { showStatus("List: " + list.getSelectedItem()); } }; // Scrollbar listener class MyScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent event) { showStatus("Scrollbar: " + scrollbar.getValue()); } }; // TextField listeners (two of them) class MyTextFieldListeners implements ActionListener, TextListener { public void actionPerformed(ActionEvent event) { showStatus("TextField(ActionListener): " + textField.getText()); } public void textValueChanged(TextEvent event) { showStatus("TextField(TextListener): " + textField.getText()); } }; // TextArea listener class MyTextAreaListener implements TextListener { public void textValueChanged(TextEvent event) { showStatus("TextArea: " + textArea.getText()); } }; // Button listener -- to demonstrate changing values programmatically class MyChangeButtonListener implements 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); } }; // CheckboxGroup listener class MyCheckboxGroupListener implements ItemListener { public void itemStateChanged(ItemEvent event) { Checkbox d = checkboxGroup.getSelectedCheckbox(); showStatus("CheckboxGroup: " + d.getLabel()); } } }