0

如何修改 StyledEditorKit 并覆盖 defaultKeyTyped 操作?我创建了一个 TextAction,甚至扩展了 StyledEditorKit。但是,如何将操作添加到扩展 StyledEditorKit 内的操作列表中呢?

最终,我试图覆盖 defaultKeyTyped 操作。我可以通过添加一个关键的监听器来做到这一点,但我不应该用编辑器工具包来做到这一点吗?从架构上讲,这更接近为更高级别的操作执行的操作,不是吗?而 addKeyListener 是一个较低级别的方法?

4

2 回答 2

2

不,我不认为这是对 EditorKit 的正确使用。更好的做法是使用 DocumentListener 或 DocumentFilter,具体取决于您是要在输入提交到 Document 之前还是之后对其进行操作。此外,另一种选择是考虑使用 InputVerifier。


编辑
你状态:

我想在键入空格键后搜索正则表达式并根据这些正则表达式突出显示文本

我自己我会为此使用 DocumentListener,但一定要在我的代码对文档进行更改时关闭侦听器,然后再将其重新打开。

于 2013-09-22T23:01:18.047 回答
1

这是我很久以前在网上找到的一些旧代码。

在顶部文本区域粘贴一些代码。在文本字段中键入正则表达式。您可以在键入或单击按钮时进行搜索,具体取决于复选框的状态。

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

import java.awt.*;
import java.awt.event.*;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexKing implements ActionListener, DocumentListener
{

    public static void main(String[] args)
    {
        new RegexKing();
    }

    public RegexKing()
    {
        initGUI();
        setupGUI();
        displayFrame();

    }

    public void initGUI()
    {
        // init
        frame = new JFrame("Regex King");
        container = new JPanel();

        inputArea = new JTextArea();
        regexField =new JTextField();
        outputArea = new JTextArea();

        quickMatch = new JCheckBox("Attempt Match on Change");
        matchButton = new JButton("Match");

        inputScroll = new JScrollPane(inputArea);
        outputScroll = new JScrollPane(outputArea);

        // setup
        outputArea.setEditable(false);
        matchButton.addActionListener(this);
        regexField.getDocument().addDocumentListener(this);

        // key binding
        KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, Event.SHIFT_MASK, false);
        Action keyact = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                doMatch();
            }
        };

        container.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "DO_MATCH");
        container.getActionMap().put("DO_MATCH", keyact);

    }

    public void setupGUI()
    {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();

        container.setLayout(gbl);

        setInsets(2, 2, 2, 2);
        gbcf = GridBagConstraints.BOTH;

        setComp(inputScroll, 10, 10, 20, 1, 1, 1);
        setComp(quickMatch, 10, 20, 1, 1, 1, .1);
        setComp(matchButton, 20, 20, 1, 1, 1, .1);
        setComp(regexField, 10, 30, 20, 1, 1, .1);
        setComp(outputScroll, 10, 40, 20, 1, 1, 1);
    }

    public void setComp(JComponent comp, int gx, int gy, int gw, int gh, double wx, double wy)
    {
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;

        gbc.fill = gbcf;

        gbl.setConstraints(comp, gbc);
        container.add(comp);
    }

    public void setInsets(int top, int bottom, int left, int right)
    {
        gbc.insets.top = top;
        gbc.insets.bottom = bottom;
        gbc.insets.left = left;
        gbc.insets.right = right;
    }

    public void displayFrame()
    {
        frame.setContentPane(container);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void insertUpdate(DocumentEvent e)
    {
        if(quickMatch.isSelected() == true)
        {
            doMatch();
        }
    }

    public void removeUpdate(DocumentEvent e)
    {
        if(quickMatch.isSelected() == true)
        {
            doMatch();
        }
    }

    public void changedUpdate(DocumentEvent e)  {}


    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource() == matchButton)
        {
            doMatch();
        }
    }

    public void doMatch()
    {
        outputArea.setText("\nAttempting Match...");
        inputArea.getHighlighter().removeAllHighlights();

        try
        {

            String inputText = inputArea.getText();
            Pattern pattern = Pattern.compile(regexField.getText());
            Matcher matcher = pattern.matcher(inputText);

            int matchCount = 0;

            while(matcher.find())
            {

                matchCount++;
                outputArea.append("\n\nMatch #" + matchCount);

                for (int i = 0; i < matcher.groupCount() + 1; i++)
                {
                    outputArea.append("\nGroup #" + i + ": " + matcher.group(i));
                    outputArea.append(" , " + matcher.end(i));
                    int start = matcher.start(i);
                    int end = matcher.end(i);
                    System.out.println(start + " : " + end);
                    inputArea.getHighlighter().addHighlight( start, end, DefaultHighlighter.DefaultPainter );

                    System.out.println(i);
                    if (matchCount == 1) inputArea.setCaretPosition(start);
                }

            }

        }
        catch(Exception exc)
        {
            outputArea.append("\nEXCEPTION THROWN");
        }

        outputArea.append("\n\nFinished.\n");

    }

    GridBagLayout gbl;
    GridBagConstraints gbc;
    int gbcf;

    JFrame frame;
    JPanel container;

    JTextArea inputArea;
    JTextField regexField;
    JTextArea outputArea;

    JScrollPane inputScroll;
    JScrollPane outputScroll;

    JCheckBox quickMatch;
    JButton matchButton;

}
于 2013-09-23T16:05:25.100 回答