我有一个 MVC 模型,我试图向它添加一个撤消/重做功能。用户可以在 JTextField 中放置一个数字,当他们单击 UNDO 按钮时,它应该被删除。我已经为实现这一点的类提供了代码。我得到的错误在底部。
控制包:
ButtonnGUIControl.java
包装控制;
/** * 该类将控制 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);
在扩展 JTextField 的 Grid.java 中:
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(grid) 中,但是一旦我点击“撤消”,我就会得到以下异常:
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 块中。
知道有什么问题吗?是我的撤消/重做实现还是其他?
谢谢!!!