Java Applet Getting Started 15

zhaozj2021-02-11  217

Getting started with Java Applet

YY435

Pacific Network Academy

Third day

6. button

The button is a very important basic component in the graphical interface, buttons do not display information, which generally corresponds to a pre-defined functional operation, which is usually represented by a program. When the user clicks the button, the system automatically performs the program associated with the button to complete the pre-specified function. Please see the example below:

Example: Submit: Cancel:

To make the above buttons, please see the syntax below:

... // That is the same as the previous example 1. Button B = New Button ("Submit"); 2. B.AddActionListener (this); 3. Button C = New Button ("Cancel"); 4. C.AddActionListener THIS); 5. Add (b); 6. Add (c); 7. Public void actionPerformed (ActionEvent E) 8. {9.} ... // The same as the previous example

analysis:

Line 1: Call the function button to establish the first button B, name is "submit";

Line 2: Add the monitor function for the Submit button B to respond to its event changes.

Chapter 3: Call the function Button to establish the second button C, name is "canceled";

Chapter 4: Add the monitor function to cancel the button C to respond to its event changes.

Chapter 5: Set the "Submit button B" into the container to make it displayed on the screen.

Chapter 6: Similarly, the "cancel button C" is added to the container to enable it to display it on the screen.

The button can trigger an action time, which triggers an action event when the user clicks on a button.

I hope that the program of the action event initiated by the button must register the button to implement ActionPerformed

ActionEvent E) Method Book Design. In the method body, an E.GetSource () method can be invoked to get

Introducing the button object of the action event, or call the E.GetActionCommand () method to the past

Now the command name set for this format. The getLabel () method of the call button can return the tag of the button;

The SetLabel (String S) method of the call button can set the tag of the button to S; call

SetActionCommand () method can set a command name for the button, this command name is in response to the event

It may be used; call the getActionCommand () method to get this command name. Pay attention to:

Some components must be placed in a single container to play, and the basic components are used to prevent the container.

Is the add () method of the container.

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

New Post(0)