• 专门用来查找Swing组件中addListener的方法(工具类)


    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);
                }
            });
        }
    }

    I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
  • 相关阅读:
    Android开发-API指南-<uses-configuration>
    Android开发-API指南-<supports-screens>
    Android开发-API指南-<supports-gl-texture>
    Android开发-API指南-<service>
    Android开发-API指南-<receiver>
    Android开发-API指南-<provider>
    Android开发-API指南-<permission-tree>
    maven scope 以及依赖传递
    转发和重定向
    Apache
  • 原文地址:https://www.cnblogs.com/IamThat/p/2980870.html
Copyright © 2020-2023  润新知