Java 如何修改StyledEditorKit并重写defaultKeyTyped操作?

Java 如何修改StyledEditorKit并重写defaultKeyTyped操作?,java,swing,oop,Java,Swing,Oop,如何修改StyledEditorKit并重写defaultKeyTyped操作? 我创建了一个TextAction,甚至扩展了StyledEditorKit。但是,如何将该操作添加到扩展样式编辑器工具包中的操作列表中呢 最终,我试图覆盖defaultKeyTyped操作。我可以通过添加一个键侦听器来实现这一点,但我不应该使用编辑器工具包来实现吗?在架构上,这更接近于为更高级别的操作执行的操作,不是吗?addKeyListener是一种较低级别的方法?不,我认为这不是EditorIt的正确用法。更

如何修改StyledEditorKit并重写defaultKeyTyped操作? 我创建了一个TextAction,甚至扩展了StyledEditorKit。但是,如何将该操作添加到扩展样式编辑器工具包中的操作列表中呢


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

不,我认为这不是EditorIt的正确用法。更好的做法是使用DocumentListener或DocumentFilter,具体取决于您是否希望在输入提交到文档之前或之后对其进行操作。另外,另一个选择是考虑使用一个输入验证程序。
编辑
你说:


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


对于我自己,我会使用DocumentListener,但是当我的代码对文档进行更改时,一定要关闭侦听器,然后再打开它。

不,我认为这不是EditorIt的正确用法。更好的做法是使用DocumentListener或DocumentFilter,具体取决于您是否希望在输入提交到文档之前或之后对其进行操作。另外,另一个选择是考虑使用一个输入验证程序。
编辑
你说:


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


对于我自己,我会使用DocumentListener,但是当我的代码对文档进行更改时,一定要关闭侦听器,然后再次打开它。

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

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

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;

}
import javax.swing.*;
导入javax.swing.event.*;
导入javax.swing.text.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.regex.Pattern;
导入java.util.regex.Matcher;
公共类RegexKing实现ActionListener、DocumentListener
{
公共静态void main(字符串[]args)
{
新RegexKing();
}
公共RegexKing()
{
initGUI();
setupGUI();
displayFrame();
}
public void initGUI()
{
//初始化
frame=新JFrame(“Regex-King”);
container=newjpanel();
inputArea=新的JTextArea();
regexField=new JTextField();
outputArea=新的JTextArea();
quickMatch=新的JCheckBox(“更改时尝试匹配”);
matchButton=新的JButton(“匹配”);
inputScroll=新的JScrollPane(inputArea);
outputScroll=新的JScrollPane(outputArea);
//设置
outputArea.setEditable(假);
matchButton.addActionListener(此);
regexField.getDocument().addDocumentListener(此);
//密钥绑定
击键键=击键.getKeyStroke(KeyEvent.VK\u ENTER,Event.SHIFT\u MASK,false);
Action keyact=new AbstractAction()
{
已执行的公共无效操作(操作事件e)
{
doMatch();
}
};
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key,“DO_MATCH”);
container.getActionMap().put(“DO_MATCH”,keyact);
}
公共void setupGUI()
{
gbl=新的GridBagLayout();
gbc=新的GridBagConstraints();
容器布局(gbl);
设置插图(2,2,2,2);
gbcf=GridBagConstraints.BOTH;
setComp(输入滚动,10,10,20,1,1,1);
setComp(快速匹配,10,20,1,1,1,1,1,1);
setComp(匹配按钮,20,20,1,1,1,1,1,1);
setComp(regexField,10,30,20,1,1,1,1);
setComp(输出滚动,10,40,20,1,1,1);
}
公共无效集合组件(JComponent组件组件、int gx、int gy、int gw、int gh、双wx、双wy)
{
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
gbc.fill=gbcf;
gbl.setConstraints(公司、gbc);
容器。添加(comp);
}
公共无效集合插图(整数顶部、整数底部、整数左侧、整数右侧)
{
gbc.insets.top=顶部;
gbc.insets.bottom=底部;
gbc.insets.left=左;
gbc.insets.right=右侧;
}
公共void displayFrame()
{
frame.setContentPane(容器);
框架。设置尺寸(500500);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共作废插入更新(文档事件e)
{
if(quickMatch.isSelected()==true)
{
doMatch();
}
}
公共作废移除更新(文档事件e)
{
if(quickMatch.isSelected()==true)
{
doMatch();
}
}
public void changedUpdate(DocumentEvent e){}
已执行的公共无效操作(操作事件evt)
{
if(evt.getSource()==matchButton)
{
doMatch();
}
}
公共无效域匹配()
{
outputArea.setText(“\n诱人的匹配…”);
inputArea.getHighlighter().removeAllHighlights();
尝试
{
字符串inputText=inputArea.getText();
Pattern=Pattern.compile(regexField.getText());
Matcher Matcher=pattern.Matcher(inputText);
int matchCount=0;
while(matcher.find())
{
matchCount++;
outputArea.append(“\n\n匹配#”+匹配计数);
对于(int i=0;i