Java applet Getting Started 12

zhaozj2021-02-11  195

Getting started with Java Applet

YY435

Pacific Network Academy

Third day

3. Drop-down list (choice) and list (list)

1. drop-down list:

What is the above example uses Java? Its syntax is as follows:

Import java.awt. *;

Import java.applet. *;

Import java.awt.event. *;

Public Class Liebiao1 Extends Applet Implements ItemListener

{

Public void init ()

{

Choice c = new choice ();

C.ADD ("----- Initial Option ------");

C.ADD ("first option");

C.ADD ("second option");

C.ADD ("Third Selection");

C.ADD ("Fourth Selection");

C.Additemlistener (this);

Add (c);

}

Public Void ItemStateChanged (ItemEvent E)

{

}

}

analysis:

Chapter 8: Call the function Choice to create a new drop-down list C

Chapter 9: Call C.Add Establishing 1st Option, Name "---- Initial Option ----", this is

The default status, that is, the option does not have the option on the screen. Its serial number is 0

Chapter 10: Establishing the 2nd option, the name "First Option" is actually the second

Options, this is a little bit of skill. Its serial number is 1. This is believed that everyone can

Better understanding.

Chapter 15: When the user clicks on the selection, it will cause ITEMEVENT.

The selection event of class representatives. The listener of the itemEvent event will be called automatically.

This method changes in response to the status of the drop-down list. GetSelectedIndex () method

The serial number of the selected option will be obtained, and the getSelectedItem () method will be selected option.

Label text string. SELECT (int index) method and SELECT (STRING ITEM) party

The method makes the program to select the options for specifying the serial number or text content. The drop-down list can generate itemEvent

Representative's selection event, if you register the option to the listener that implements the excuses itemListener:

C.Additemlistener (). When the user clicks an option to the drop-down list, the system is automatically produced.

An object of an ItemEvent class contains information about this event.

Chapter 16: Add the drop-down list C to the container F to make it displayed on the screen.

2. List: (can be multi-selection)

What is the above example uses Java? Its syntax is as follows:

1. .......... // The same as the previous example 2. List year = new List (4, true); 3. Year.Add ("1998"); 4. Year.Add ("1999"); 5. Year.Add ("2000"); 6. Year.Add ("2001"); 7. Add (year); 8. ............ // The same as the previous example

analysis:

Line 2: Call the function list to establish a new multi-selection list year. It has four options,

TRUE indicates that this table can be multi-sept. When it is false, it can only be selected.

Chapter 3: Call Year.Add to establish the first option, name "1998", its serial number is 0

Chapter 4: Establishing the 2nd option, the name is "1999". Its serial number is 1.

Chapter 8: Add the drop-down list C to the container F to make it displayed on the screen. If you want or know which option in the list is selected, you can call the list object:

GetSelectedItems () method, this method returns an array of string types, inside

Each element is an option selected by the user, and all elements include all selected by users.

Options. This method returns the selection option. The list can have multiple selection and radio. Instant

There is also a method in the List object GeetSelectItem () method, which is different from the radio. this way

Returns a single string.

In addition to returning the tag string selected, you can also get the serial number of the selected option. in

Inside, the getSelectIndex () method will return the selected options and serial numbers.

The GetSelectIndexs () method will get an integrated array that consists of the sequence number of the selected option.

转载请注明原文地址:https://www.9cbs.com/read-4737.html

New Post(0)