The
Drop-down list is like a radio button group, which is a way to force a user to select an object from a set of achievable selections. Moreover, it is a fairly concise way to achieve this, and it is easiest to change the choice without straining the user (we can dynamically change the radio button, but that approach is obviously inconvenient). The Java selection box is not like a combo box in Windows that allows me to select from a list or enter my own selection. In a selection box you can select only one item from the list. In the following example, the selection box starts with a number that determines the input, and then when a button is pressed, the newly entered number is added to the box. You will be able to see some interesting states of the selection box:
//: Choice1.java//Using drop-down lists import java.awt.*; import java.applet.*; public class Choice1 extends Applet {string[] description = {"Ebullient", "obtuse", "recalcitrant", "brilliant", "
Somnescent "," timorous "," florid "," putrescent "};
TextField t = new TextField (30);
Choice C = new Choice ();
Button B = New button ("Add items");
int count = 0;
public void init () {t.seteditable (false);
for (int i = 0; i < 4; i++) C.additem (description[count++]);
Add (t);
Add (c);
Add (b); Public boolean action (Event evt, Object Arg) {if (Evt.target.equals (c)) T.settext ("index:" + c.getselecte
Dindex () + "" + (String) arg);
else if (Evt.target.equals (b)) {if (Count < Description.length) C.additem (description[count++));
else return super.action (EVT, ARG);
return true; }
} ///:~
The "Selected index," shown in the Text field, is the serial number of the currently selected item, and the string selected in the event is as good as the character of the second argument of the action ().
When you run this program, notice the size of the choice box: In Windows, this size is determined when we pull down the list. This means that if we pull the following table and then add more items to the list, this item will be there, but this dropdown list is no longer acceptable (we can scroll through the project to observe--note ④). However, if we mount the item in the Drop-down list before we pull down the Drop-down list for the first time, it will be the right size. Of course, users want to see the entire list when they use it, so they can make a special qualification for adding items to the selection box in the state of the Drop-down list.
④: This behavior is clearly a mistake that will be resolved in a later version of Java.