摆动撤消选项

Swing Undo Option

我一直在看 undo/redo 选项的教程 Java Swing 图形用户界面。我不知道我做错了什么。以下代码是另一个 class 中的内部 class。提交按钮工作正常。单击时撤消选项不会撤消。有人可以让我知道我做错了什么吗?谢谢

private class listen implements ActionListener
{
    UndoManager manager;
    Document doc;
    @Override
    public void actionPerformed( ActionEvent e ) 
    {
        int result;
        Calendar times = Calendar.getInstance();
        createFile file = new createFile();
        manager = new UndoManager();
        doc = area.getDocument();
        String output_1, output_2, output_3, fileName;
        search find = new search();
        JPanel panel = new JPanel();
        JTextField input_1 = new JTextField(15);
        JTextField input_2 = new JTextField(15);
        JTextField input_3 = new JTextField(15);
        JLabel word_1 = new JLabel("KeyWord:");
        JLabel word_2 = new JLabel("KeyWord:");
        JLabel word_3 = new JLabel("KeyWord:");
        panel.add(word_1);
        panel.add(input_1);
        panel.add(Box.createHorizontalStrut(20));
        panel.add(word_2);
        panel.add(input_2);
        panel.add(Box.createHorizontalStrut(20));
        panel.add(word_3);
        panel.add(input_3);

        if(e.getSource() == submit)
        {
            do
            {
                result = JOptionPane.showConfirmDialog(null, panel,
                        "KeyWords", JOptionPane.OK_CANCEL_OPTION);
                output_1 = input_1.getText();
                output_2 = input_2.getText();
                output_3 = input_3.getText();
                if( result == JOptionPane.OK_OPTION
                        && !find.isValidWord(output_1, output_2, output_3) )
                {
                    JOptionPane.showMessageDialog(null, "<html>You must "
                            + "enter atleast 1 KeyWord, and also all<br> "
                            + "valid KeyWords must contain atleast one "
                            + "alphabetic letter,</html>");
                }
            }
            while(result == JOptionPane.OK_OPTION && 
                    !find.isValidWord(output_1, output_2, output_3));
            if(result == JOptionPane.OK_OPTION)
            {
                fileName = times.get(Calendar.MONTH) + "_" + 
                        times.get(Calendar.DAY_OF_MONTH + 1) + "_" + 
                        times.get(Calendar.YEAR) + "_" + output_1 + "_" +
                        output_2 + "_" + output_3 + "_";
                file.file(fileName, area.getText());
            }             
        }
        else if(e.getSource() == undo)
        {
            doc.addUndoableEditListener( new UndoableEditListener()
            {
                @Override
                public void undoableEditHappened( UndoableEditEvent e1 )
                {
                    manager.addEdit(e1.getEdit());
                    update();
                }
            });
            try
            {
                if( manager.canUndo() )
                    manager.undo();
            } catch(CannotUndoException exp) {}
        }
    }
    public void update()
    {
        undo.setEnabled(manager.canUndo());
    }
}   

I have been watching tutorials -

与其看教程,不如从 Implementing Undo/Redo 上的 Swing 教程下载的工作代码开始。

我猜你需要在创建文本组件时将 UndoableEditListener 添加到文档中,而不是在 ActionListener 中。否则,不会生成编辑,因为文本为 added/removed.