加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java Swing实现的正则表达式测试工具

发布时间:2020-12-15 00:18:59 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.awt.GridLayout;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;impor

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.awt.GridLayout;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * Standalone Swing GUI application for demonstrating REs. <br/>TODO: Show the
 * entire match,and $1 and up as captures that matched.
 */

public class GuiDemo extends JPanel {

    protected Pattern pattern;
    protected Matcher m;
    protected JTextField pattTF,strTF;
    protected JCheckBox compiledOK;
    protected JRadioButton match,find,findAll;
    protected JTextField mTF;

    /**
     * "main program" method - construct and show
     */
    public static void main(String[] av) {

        JFrame f = new JFrame("GuiDemo");

        f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        GuiDemo comp = new GuiDemo();
        f.setContentPane(comp);
        f.pack();
        f.setLocation(200,200);
        f.setVisible(true);
    }

    /**
     * Construct the REDemo object including its GUI
     */
    public GuiDemo() {
        super();

        JPanel top = new JPanel();
        top.add(new JLabel("Pattern:",JLabel.RIGHT));
        pattTF = new JTextField(20);
        pattTF.getDocument().addDocumentListener(new PattListener());
        top.add(pattTF);
        top.add(new JLabel("Syntax OK?"));
        compiledOK = new JCheckBox();
        top.add(compiledOK);

        ChangeListener cl = new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent ce) {
                tryToMatch();
            }
        };

        JPanel switchPane = new JPanel();
        ButtonGroup bg = new ButtonGroup();
        match = new JRadioButton("Match");
        match.setSelected(true);
        match.addChangeListener(cl);
        bg.add(match);
        switchPane.add(match);
        find = new JRadioButton("Find");
        find.addChangeListener(cl);
        bg.add(find);
        switchPane.add(find);
        findAll = new JRadioButton("Find All");
        findAll.addChangeListener(cl);
        bg.add(findAll);
        switchPane.add(findAll);

        JPanel strPane = new JPanel();
        strPane.add(new JLabel("String:",JLabel.RIGHT));
        strTF = new JTextField(20);
        strTF.getDocument().addDocumentListener(new StrListener());
        strPane.add(strTF);
        strPane.add(new JLabel("Matches:"));
        mTF = new JTextField(3);
        strPane.add(mTF);

        setLayout(new GridLayout(0,1,5,5));
        add(top);
        add(strPane);
        add(switchPane);
    }

    protected void setMatch(boolean b) {
        if (b) {
            mTF.setText("Yes");
        } else {
            mTF.setText("No");
        }
    }

    protected void setMatch(int n) {
        mTF.setText(Integer.toString(n));
    }

    protected void tryToCompile() {
        pattern = null;
        try {
            pattern = Pattern.compile(pattTF.getText());
            m = pattern.matcher("");
            compiledOK.setSelected(true);
        } catch (PatternSyntaxException ex) {
            compiledOK.setSelected(false);
        }
    }

    protected boolean tryToMatch() {
        if (pattern == null) {
            return false;
        }
        m.reset(strTF.getText());
        if (match.isSelected() && m.matches()) {
            setMatch(true);
            return true;
        }
        if (find.isSelected() && m.find()) {
            setMatch(true);
            return true;
        }
        if (findAll.isSelected()) {
            int i = 0;
            while (m.find()) {
                ++i;
            }
            if (i > 0) {
                setMatch(i);
                return true;
            }
        }
        setMatch(false);
        return false;
    }

    /**
     * Any change to the pattern tries to compile the result.
     */
    class PattListener implements DocumentListener {

        @Override
        public void changedUpdate(DocumentEvent ev) {
            tryToCompile();
        }

        @Override
        public void insertUpdate(DocumentEvent ev) {
            tryToCompile();
        }

        @Override
        public void removeUpdate(DocumentEvent ev) {
            tryToCompile();
        }
    }

    /**
     * Any change to the input string tries to match the result
     */
    class StrListener implements DocumentListener {

        @Override
        public void changedUpdate(DocumentEvent ev) {
            tryToMatch();
        }

        @Override
        public void insertUpdate(DocumentEvent ev) {
            tryToMatch();
        }

        @Override
        public void removeUpdate(DocumentEvent ev) {
            tryToMatch();
        }
    }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读