我有一个MVC模型,我试图添加一个撤消/重做功能。 用户可以在JTextField中放置一个数字,当他们单击UNDO按钮时,它应该被删除。 我已经为我的类提供了实现它的代码。我得到的错误在底部。
控制包:
包裹控制;
/ ** *此类将控制GUI的按钮操作。它实现actionListener()方法 *并根据点击的按钮,初始化 *模型类中的相应组件。 * * /
public class ButtonGUIControl implements ActionListener{
private SudokuModel model;
private ButtonGUI bgui;
final UndoManager manager = new UndoManager();
public ButtonGUIControl(SudokuModel model) {
this.model = model;
}
/*This method will listen for user events, and act accordingly
* */
public void actionPerformed(ActionEvent e) {
try {
if(e.getActionCommand().equals("Check My Answer!")){
model.checkGame();
}
else if (e.getActionCommand().equals("Undo")){
try {
manager.undo();
} catch (CannotUndoException ex) { ex.printStackTrace(); }
finally {
//updateButtons();
}
}
else if (e.getActionCommand().equals("Redo")){
manager.redo();
System.out.println("e.getActionCommand().equals redo");
}
//otherwise, it simply places the selected number in the grid
else{
model.setNumber(Integer.parseInt(e.getActionCommand()));
}
} catch (NumberFormatException e1) {}
catch (IOException e1) {}
catch (CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
catch (CannotRedoException ex) {
System.out.println("Unable to redo: " + ex);
ex.printStackTrace();
}
}
public void undoableEditHappened(UndoableEditEvent evt) {
manager.addEdit(evt.getEdit());
//bgui.updateButtons();
}
}
在视图包中: ButtonGUI.java
我在按钮上添加了一个actionListner:
RedoBttn.addActionListener(buttonController);
UndoBttn.addActionListener(buttonController);
在Grid.java中,它扩展了JTextField:
public class Grid extends JTextField {
private int x;
private int y;
final UndoManager manager = new UndoManager();
public Grid(int x, int y) {
super("");
this.x = x;
this.y = y;
getDocument().addUndoableEditListener(manager);
setPreferredSize(new Dimension(40, 40));
setBorder(BorderFactory.createLineBorder(Color.GREEN));
setOpaque(true);
}
public void setNumber(int number, boolean userInput) {
if(number > 0 & !userInput){
setText(""+number);
setEditable(false);
}
//These set of numbers are the only ones that the user can undo/redo
else if (number > 0 & userInput){
setText(""+number);
setEditable(true);
}
else
setText("");
我还在视图包中的另一个类的每个JTextField中添加了一个鼠标监听器。 所以我认为我的撤销/重做结构正确。 当我运行程序时,我可以选择一个数字并将其放在JTextField(网格)中,但只要我点击“撤消”,我就会得到以下异常:
javax.swing.undo.CannotUndoException
at javax.swing.undo.UndoManager.undo(UndoManager.java:411)
at control.ButtonGUIControl.actionPerformed(ButtonGUIControl.java:76)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
和第76行是我打电话的地方:
manager.undo()
在try / catch块中。
任何想法都错了吗?是我的撤销/重做实现还是其他什么?
感谢!!!
答案 0 :(得分:3)
尝试这样包装manager.undo();
:
if(manager.canUndo())
manager.undo();
我怀疑JTextField
的默认UndoListener实现没有添加系统更新(即调用setText(""+number);
到撤消队列) - 只是来自UI的更新。
修改强>
在进行一些测试之后,似乎setText("")
函数确实向UndoManager
添加了撤消操作,因此这不是您遇到的问题。因此,您可以通过确保撤消任何可撤消的操作来防止错误;但是,如果您未在UndoManager
中获得任何可撤消操作,则问题可能发生在代码中的其他位置。
答案 1 :(得分:1)
作为替代方案,请考虑JFormattedTextField
。默认情况下, reset-field-edit 操作绑定到 ESC 键,但您可以根据需要将操作绑定到另一个键,例如KeyEvent.VK_Z
,如How to Use Actions所示。要获得跨平台兼容性,请使用getMenuShortcutKeyMask()
,提及here。