我需要寫這3個函數,但是我被卡在重做和刪除。當沒有任何重做時,重做顯示錯誤,我不知道如何編寫刪除功能。謝謝在java中撤消,重做,刪除函數
撤消
public class Undo extends AbstractAction {
private MyCanvas myCanvas;
public Undo(MyCanvas myCanvas) {
this.myCanvas = myCanvas;
this.putValue(NAME, "Undo");
this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z"));
this.putValue(SMALL_ICON, new ImageIcon(Main.class.getResource("/icons/Undo24.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!myCanvas.commands.isEmpty()) {
Command cmd = myCanvas.commands.pop();
cmd.undo();
myCanvas.undoneCommands.add(cmd);
myCanvas.repaint();
}
else
System.out.println();
}
}
重做
public class Redo extends AbstractAction {
private MyCanvas myCanvas;
public Redo(MyCanvas myCanvas) {
this.myCanvas = myCanvas;
this.putValue(NAME, "Redo");
this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y"));
this.putValue(SMALL_ICON, new ImageIcon(Main.class.getResource("/icons/Redo16.gif")));
}
public void actionPerformed(ActionEvent e) {
//if (!myCanvas.commands.isEmpty()) {
Command cmd = myCanvas.undoneCommands.pop();
cmd.execute();
myCanvas.commands.add(cmd);
myCanvas.repaint();
//}
// else
System.out.println();
}
}
無關的,但爲什麼你把方法調用的前面?它不表示任何東西。 – 2010-01-12 20:16:37
爲了確保代碼被格式化爲代碼,在其前面至少放置四個空格。 (我進去了,爲你改變了這個。) – 2010-01-19 09:17:54