Swing撤消选项

时间:2016-02-24 21:07:51

标签: java swing undo

我一直在观看有关undo / redo选项的教程 用于Java Swing GUI。我无法弄清楚我做错了什么。以下代码是另一个类中的内部类。提交按钮工作正常。单击时,撤消选项不会撤消。有人能让我知道我做错了什么吗?感谢。

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());
    }
}   

1 个答案:

答案 0 :(得分:1)

  

我一直在看教程 -

不要看教程,而是从可以在Implementing Undo/Redo上的Swing教程中下载的代码开始。

我猜你需要在创建文本组件时将UndoableEditListener添加到Document中,而不是在ActionListener中。否则,在添加/删除文本时不会生成编辑。