Introduction to the AWT event model

xiaoxiao2021-03-06  68

Make a component that event handles can be roughly divided into the following three steps: 1. Public class myclass meansListener {} 2, someComponent.addActionListener (InstanceOfmyClass); 3, public void actionPerformed (ActionEvent E) {... // event execution Code} first is a simple example (it is really simple, but there is an applet, sun true metamorphosis)

IMPORT JAVA.Applet.Applet;

Import java.awt.button;

Import java.awt.toolkit;

Import java.awt.BorderLayout;

Import java.awt.event.ActionListener;

Import java.awt.event.Actionevent;

Public Class Beeper Extends Applet Implements ActionListener {

Button button;

Public void init () {

SetLayout (New BorderLayout ());

Button = New Button ("Click ME");

Add ("Center", Button;

Button.addActionListener (this);

}

Public Void ActionPerformed (ActionEvent E) {

Toolkit.getDefaulttoolkit (). Beep ();

}

} The Beeper here first implements ActionListener, which is ringing in method actionPerformed. So it can register as a Button's ActionListener. Next is an example of a complex point. IMPORT JAVA.Applet.Applet;

Import java.awt. *;

Import java.awt.event.ActionListener;

Import java.awt.event.Actionevent;

Public Class Multilistener Extends Applet Implements ActionListener {

Textarea ToptextArea;

Textarea BottomtextArea;

Button button1, button2;

Public void init () {

Label L = NULL;

GridbagLayout Gridbag = new gridbaglayout ();

Gridbagconstraints c = new gridbagconstraints ();

SetLayout (gridbag);

C.fill = Gridbagconstraints.both;

C.GridWidth = Gridbagconstraints.Remainder;

L = New label ("What Multilistener Hears:");

Gridbag.setconstraints (L, C);

Add (L);

C.WeighTy = 1.0;

ToptextArea = New Textarea (5, 20);

ToptextArea.setedITable (FALSE);

Gridbag.setConstraints (ToptextArea, C);

Add (ToptextArea);

C.Weightx = 0.0;

C.WeighTy = 0.0;

L = New Label ("What EavesDropper Hears:"); Gridbag.SetConstraints (L, C);

Add (L);

C.WeighTy = 1.0;

BottomtextArea = New Textarea (5, 20);

BottomtextArea.setedItable (false);

Gridbag.SetConstraints (BottomtextArea, C);

Add (BottomtextArea);

C.Weightx = 1.0;

C.WeighTy = 0.0;

C.GridWidth = 1;

C.insets = new INSETS (10, 10, 0, 10);

Button1 = New Button ("Blah Blah Blah");

Gridbag.setConstraints (Button1, C);

Add (Button1);

C.GridWidth = Gridbagconstraints.Remainder;

Button2 = New Button ("you don't say!");

Gridbag.setconstraints (Button2, C);

Add (Button2);

Button1.addactionListener (this);

Button2.addactionListener (this);

Button2.addActionListener (New EavesDropper);

}

Public Void ActionPerformed (ActionEvent E) {

ToptextArea.Append (E.GetActionCommand () "/ n");

}

}

Class EavesDropper IMPLEments ActionListener {

Textarea mytextarea;

Public EavesDropper (TextArea TA) {

Mytextarea = TA;

}

Public Void ActionPerformed (ActionEvent E) {

MytextArea.Append (E.GetActionCommand () "/ n");

}

} This is here to use a complex layout manager, true BT, complicate simple problem. Then add the actionListener to Button1 and Button2, respectively. And I am moving E.GetActionCommand () to get the command text, which is of course the text on the button. The text is given to the TEXTAREA located on the top layer, and then registered with Button2 a EavesDropper that implements the ActionListener interface, and the constructor is passed a TextArea object that saves the lower side and uses the object to get the event source. text. Overall, this example is still, it is complex. Finally, an example of a mouse event is relatively simple, not coming. ^ _ ^ Import java.awt. *;

Public Class Blankarea Extends Canvas {

Dimension Minsize = New Dimension (100, 100);

Public Blankarea (Color Color) {

SetBackground (Color);

}

Public Dimension GETMINIMUMUMSIZE () {

Return minsize;

}

Public Dimension GetPreferRedsize () {Return Minsize;

}

Public void paint (graphics g) {

Dimension size = getsize ();

g.drawRect (0, 0, Size.width - 1, Size.height - 1);

}

}

IMPORT JAVA.Applet.Applet;

Import java.awt. *;

Import java.awt.event.mouselistener;

Import java.awt.event.mouseevent;

Public class mouseeventdemo extends applet imports mouselistener {

Blankarea blankarea;

Textarea Textarea;

Static final int maxint = java.lang.integer.max_value;

String newline;

Public void init () {

GridbagLayout Gridbag = new gridbaglayout ();

Gridbagconstraints c = new gridbagconstraints ();

SetLayout (gridbag);

C.fill = Gridbagconstraints.both;

C.GridWidth = Gridbagconstraints.Remainder;

C.Weightx = 1.0;

C.WeighTy = 1.0;

C.insets = new INSETS (1, 1, 1, 1);

Blankarea = New Blankarea (New Color (0.98F, 0.97F, 0.85F));

Gridbag.SetConstraints (Blankarea, C);

Add (Blankarea);

C.insets = new INSETS (0, 0, 0, 0);

Textarea = New Textarea (5, 20);

TextArea.setedITable (FALSE);

Gridbag.setconstraints (TextArea, C);

Add (TextArea);

// Register for Mouse Events on Blankarea and Applet (Panel).

Blankarea.addmouselistener (this);

AddMouseListener (this);

NEWLINE = System.getProperty ("line.separator");

}

Public void mousepressed (mouseevent e) {

Saysomething ("Mouse Pressed; # of Clicks:" E.GetClickCount (), E);

}

Public void mouseeleased (mouseevent e) {

Saysomething ("Mouse Released; # of clicks:" E.GETClickCount (), E);

}

Public void mouseentered (mouseevent e) {

Saysomething ("Mouse Entered", E);

}

Public void mouseexited (mouseevent e) {

Saysomething ("Mouse EXITED", E);

}

Public void mouseclicked (mouseevent e) {

Saysomething ("Mouse Clicked (# of clicks:" E.GETClickCount () ")", e);

}

Void signomething (String EventDescription, MouseEvent E) {

Textarea.Append

EventDescription

"detected on"

E.GetComponent (). getClass (). getname ()

newline);

Textarea.setcaretPosition (maxint); // Hack To Scroll To Bottom

}

}

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

New Post(0)