在我们编写JAVA代码的过程中,需要给事件源增加监听事件,可是我们往往记不住这个事件源有那些可以使用的事件,所以我参考《JAVA编程思想》,按照书上的说明,写了如下的代码,主要是能够得到事件源可以使用的事件的列表。
程序运行的界面如下:
这个程序还是有点不完善,输入的类名必须区分大小写 ,另外程序是以javaapplet运行的。
主要的代码如下:
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 actionPerformed(ActionEvent e) {
String nm = name.getText().trim();
if (nm.length() == 0 ) {
results.setText( " 没有匹配 " );
return ;
}
Class klass;
try {
klass = Class.forName( " javax.swing. " + nm);
} catch (ClassNotFoundException ex) {
results.setText( " 没有匹配 " );
return ;
}
Method[] methods = klass.getMethods();
results.setText( "" );
for ( int i = 0 ; i < methods.length; i ++ ) {
Matcher 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 类名 (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.actionPerformed(
new ActionEvent( "" , 0 , "" ));
}
public static void main(String[] args) {
Console.run( new ShowAddListeners(), 500 , 400 );
}
} // /:~
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 actionPerformed(ActionEvent e) {
String nm = name.getText().trim();
if (nm.length() == 0 ) {
results.setText( " 没有匹配 " );
return ;
}
Class klass;
try {
klass = Class.forName( " javax.swing. " + nm);
} catch (ClassNotFoundException ex) {
results.setText( " 没有匹配 " );
return ;
}
Method[] methods = klass.getMethods();
results.setText( "" );
for ( int i = 0 ; i < methods.length; i ++ ) {
Matcher 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 类名 (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.actionPerformed(
new ActionEvent( "" , 0 , "" ));
}
public static void main(String[] args) {
Console.run( new ShowAddListeners(), 500 , 400 );
}
} // /:~
里面import niutool.Console,引入了一个Console的工具,源代码如下:
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.setDefaultCloseOperation(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible( true );
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible( true );
}
} // /:~
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.setDefaultCloseOperation(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible( true );
}
public static void
run(JPanel panel, int width, int height) {
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width, height);
frame.setVisible( true );
}
} // /:~