When writing Java code, we need to add listening events to the event source, but we often cannot remember the events that can be used by this event source. Therefore, I refer to Java programming ideas, according to the instructions in the book, the following code is written to obtain a list of events that can be used by the event source.
The program running interface is as follows:
This program is still a bit imperfect. The input class name must be case-sensitive. In addition, the program runs in Java applet.
The main code is as follows:
Package niutool;
Import niutool. Console;
Import javax. Swing .*;
Import javax. Swing. event .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Lang. Reflect .*;
Import java. util. RegEx .*;
Public class showaddlisteners extends japplet {
Private jtextfield name = new jtextfield (25 );
Private jtextarea Results = new jtextarea (40, 65 );
Private Static pattern addlistener =
Pattern. Compile ("(Add/W +? Listener /(.*? /))");
Private Static pattern qualifier =
Pattern. Compile ("/W + /.");
Class namel implements actionlistener {
Public void actionreceivmed (actionevent e ){
String Nm = Name. gettext (). Trim ();
If (nm. Length () = 0 ){
Results. settext ("no matching ");
Return;
}
Class Klass;
Try {
Klass = Class. forname ("javax. Swing." + nm );
} Catch (classnotfoundexception ex ){
Results. settext ("no matching ");
Return;
}
Method [] Methods = Klass. getmethods ();
Results. settext ("");
For (INT I = 0; I <methods. length; I ++ ){
Matcher =
Addlistener. matcher (methods [I]. tostring ());
If (matcher. Find ())
Results. append (qualifier. matcher (
Matcher. Group (1). replaceall ("") + "");
}
}
}
Public void Init (){
Namel namelistener = new namel ();
Name. addactionlistener (namelistener );
Jpanel Top = new jpanel ();
Top. Add (New jlabel ("swing class name (Press ENTER ):"));
Top. Add (name );
Container CP = getcontentpane ();
CP. Add (borderlayout. North, top );
CP. Add (New jscrollpane (results ));
// Initial data and test:
Name. settext ("jtextarea ");
Namelistener. actionreceivmed (
New actionevent ("", 0 ,""));
}
Public static void main (string [] ARGs ){
Console. Run (New showaddlisteners (), 500,400 );
}
}///:~
Import niutool. console and introduce a console tool. The source code is as follows:
Package niutool;
Import javax. Swing .*;
Import java. AWT. event .*;
Public class console {
// Create a title string from the class name:
Public static String title (Object O ){
String T = O. getclass (). tostring ();
// Remove the word "class ":
If (T. indexof ("class ")! =-1)
T = T. substring (6 );
Return T;
}
Public static void
Run (jframe frame, int width, int height ){
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. setsize (width, height );
Frame. setvisible (true );
}
Public static void
Run (japplet applet, int width, int height ){
Jframe frame = new jframe (title (applet ));
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. getcontentpane (). Add (applet );
Frame. setsize (width, height );
Applet. INIT ();
Applet. Start ();
Frame. setvisible (true );
}
Public static void
Run (jpanel, int width, int height ){
Jframe frame = new jframe (title (panel ));
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. getcontentpane (). Add (panel );
Frame. setsize (width, height );
Frame. setvisible (true );
}
}///:~
There is something you don't understand. You can look at Java programming ideas.