Handle events using adapters and internal classes

xiaoxiao2021-03-06  67

Just now, we see an example of a BT, you must write a method of processing each mouse event when implementing a mouselistener interface. Here you will introduce Adapter. Easy.

IMPORT JAVA.Applet.Applet;

Import java.awt. *;

Import java.awt.event. *;

Public class mouseeventadapterdemo extends applet {

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 (new myevent ());

AddMouseListener (new myevent ());

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

}

Class myevent extends mouseadapter {

Public void mousepressed (mouseevent e) {

Saysomething ("Mouse Pressed; # 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

}

}

} No need to say.

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

New Post(0)