zoukankan      html  css  js  c++  java
  • 专门用来查找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 程序 = 数据结构 + 算法
  • 相关阅读:
    Delphi 之Copyrect的使用
    Delphi GDI对象之脱屏位图(Offscreen Bitmaps),也叫内存位图
    1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    每一个JavaScript开发者都应该知道的10道面试题
    【HDOJ 5407】 CRB and Candies (大犇推导
    Wireshark默认不抓取本地包的解决方式
    Android LaunchMode案例篇
    使用ViewPager实现广告滑动效果
    剑指offer面试题26-复杂链表的复制
  • 原文地址:https://www.cnblogs.com/IamThat/p/2980870.html
Copyright © 2011-2022 走看看