package demo01;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class ShowAddListeners extends JFrame {
/**
*
*/
private static final long serialVersionUID = 560378219087793087L;
/**
* @param args
*/
private JTextField name=new JTextField(25);
private JTextArea results=new JTextArea(40,65);
private static Pattern addListner=
Pattern.compile("(add\\w+?Listener\\(.*?\\))");
private static Pattern qualifier=Pattern.compile("\\w+\\.");
class NameL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String nm=name.getText().trim();
if(nm.length()==0){
results.setText("No Match");
return;
}
Class<?>kind;
try {
kind=Class.forName("javax.swing."+nm);
} catch (ClassNotFoundException e) {
results.setText("No Match");
return;
}
Method[] methods=kind.getMethods();
results.setText("");
for(Method m:methods){
Matcher matcher=addListner.matcher(m.toString());
if(matcher.find()){
results.append(qualifier.matcher(
matcher.group(1)).replaceAll("")+"\n");
}
}
}
}
public ShowAddListeners(){
NameL nameListener=new NameL();
name.addActionListener(nameListener);
JPanel top=new JPanel();
top.add(new JLabel("Swing class name( press enter ):"));
top.add(name);
add(BorderLayout.NORTH,top);
add(new JScrollPane(results));
name.setText("JTextArea");
nameListener.actionPerformed(new ActionEvent("", 0, ""));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingConsole.run(new ShowAddListeners(),500,400);
}
}
附带: 用于显示的工具类
package demo01;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SwingConsole {
public static void run(final JFrame f, final int width,final int height){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width,height);
f.setVisible(true);
}
});
}
}