创建一个可撤销的JTextField



我已经实现了一个可撤销的JTextField,问题是当我创建多个JTextField时,它只撤销创建的最后一个实例。代码如下-要复制,在第三个字段中输入一些文本,然后在第一个文本字段中输入一些代码,然后按ctrl-z键撤消-它只从第三个文本字段中删除文本。有人能看出我做错了什么吗?

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class UndoableTextFieldTest 
{
public static void main(String... s)
{
new UndoableTextFieldTest();
}
public UndoableTextFieldTest() 
{
JFrame frame = new JFrame("UndoableTextFieldTest");
UndoableTextField f1 = new UndoableTextField();
UndoableTextField f2 = new UndoableTextField();
UndoableTextField f3 = new UndoableTextField();
frame.setLayout(new BorderLayout());
frame.getContentPane().add(f1, BorderLayout.NORTH);
frame.getContentPane().add(f2, BorderLayout.CENTER);
frame.getContentPane().add(f3, BorderLayout.SOUTH);
frame.setSize(360, 115);
frame.setVisible(true);
}

public class UndoableTextField extends JTextField
{    
KeyStroke undoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
KeyStroke redoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
public UndoableTextField()
{
UndoManager undoManager = new UndoManager();
getDocument().addUndoableEditListener(new UndoableEditListener() 
{
@Override
public void undoableEditHappened(UndoableEditEvent e) 
{
undoManager.addEdit(e.getEdit());
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(undoKeyStroke, "undoKeyStroke");
getActionMap().put("undoKeyStroke", new AbstractAction() 
{
@Override
public void actionPerformed(ActionEvent e) 
{
try 
{
undoManager.undo();
} catch (CannotUndoException cue) {}
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(redoKeyStroke, "redoKeyStroke");
getActionMap().put("redoKeyStroke", new AbstractAction() 
{
@Override
public void actionPerformed(ActionEvent e) 
{
try 
{
undoManager.redo();
} catch (CannotRedoException cre) {}
}
});
}    
}
}

我不是100%确定,但我猜您使用的输入映射是在窗口中的所有组件之间共享的。前两个字段的放权将被第三个字段的放权覆盖。

如果我使用getInputMap(),那么撤销似乎对每个文本字段都有效。

为子孙更正的代码:

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class UndoableTextFieldTest 
{
public static void main(String... s)
{
new UndoableTextFieldTest();
}
public UndoableTextFieldTest() 
{    
KeyStroke undoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
KeyStroke redoKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);

public UndoableJTextField()
{
UndoManager undoManager = new UndoManager();

getDocument().addUndoableEditListener(new UndoableEditListener() 
{
@Override
public void undoableEditHappened(UndoableEditEvent e) 
{
undoManager.addEdit(e.getEdit());
}
});

getInputMap().put(undoKeyStroke, "undoKeyStroke");
getActionMap().put("undoKeyStroke", new AbstractAction() 
{
@Override
public void actionPerformed(ActionEvent e) 
{
try { undoManager.undo(); } 
catch (CannotUndoException cue) {}
}
});
getInputMap().put(redoKeyStroke, "redoKeyStroke");
getActionMap().put("redoKeyStroke", new AbstractAction() 
{
@Override
public void actionPerformed(ActionEvent e) 
{
try { undoManager.redo(); } 
catch (CannotRedoException cre) {}
}
});
}    
}

相关内容

  • 没有找到相关文章

最新更新