Java Applet Getting Started 10

zhaozj2021-02-11  282

Getting started with Java Applet

YY435

Pacific Network Academy

Third day

1. Checkbox

example:

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

Import java.awt. *;

Import java.applet. *;

Import java.awt.event. *;

Public Class Fuxuan Extends Applet

Implements itemlistener

{

Public void init ()

{

Checkbox One = New Checkbox ("Cat");

Checkbox Two = New Checkbox ("Dog", TRUE;

CheckBox Three = New Checkbox ("Rabbit", False;

One.Additemlistener (this);

Add (one);

Two.additemlistener (this);

Add (TWO);

Three.Additemlistener (this);

Add (three);

}

Public Void ItemStateChanged (ItemEvent E)

{

}

}

Analysis: Chapter 3: Turn into the handling package to handle the event of the check box line 4: IMplements is the interface of the event, call the interface with the imports, this interface function is itemlistener; 8th line: call function Checkbox Establishment 1 option, the default status is false, that is, no pre-selected. Chapter 9: Establishing a one in advance that has been default the correct option. Cap 12,14,16: When the user clicks the check box makes its status change, the selection event representative will be raised by the ItemEvent class. This checkbox has already registered itself to the listener itemListener of the ItemEvent event. The PUBLIC VOID ITEMSTATATECHANGED (ITEMEVENT E) method in this itemListener will automatically call the status change in the check box. Actually realizing the listener of the ItemListener interface. The method of this method typically includes such a statement: Method for calling the selection event E.GetSelectable () Gets the event source object reference for issuing the selection event, and then calls E.GETSTATE () to obtain the status after selecting the event. It is also possible to directly use the event event original object itself, but to note that the return value of the getSelectable () method is an object that implements the Selectable interface, you need to force it into a true event original object type. For example: (Checkbox) .Selectable) .GetState (): The effect is consistent with the E.GETSTATE effect. Sections 13, 15, 17: Add One, TWO, Three check box to the panel to make it displayed on the screen.

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

New Post(0)