0

我正在尝试undo button使用stack它来删除应用的效果,例如blur等,但是当我尝试存储在其中pop out的i出现问题时,请帮助我,在此先感谢!imagestack

应用效果:

if (e.getSource() == btnGrayscale) {
  if (buffImage != null) {
    Imagesteps.push(buffImage);
    ImageEffects ie = new GrayscaleEffect();

    buffImage = ie.GrayscaleEffect(buffImage);
    Icon icon = new ImageIcon(buffImage);
    lblImage.setIcon(icon);
    statusBar.setText("Image is now Grayscaled");
  }
}

UNDO方法:

if (e.getSource() == undoimageitem) {
  BufferedImage temp = (BufferedImage) Imagesteps.pop();
  Icon icon = new ImageIcon(temp);
  lblImage.setIcon(icon);
  statusBar.setText("Undo");
}
4

1 回答 1

1

从您得到的异常来看,您似乎正在尝试从空堆栈中弹出。为避免此错误,理想情况下,您应该在弹出之前确保堆栈的大小至少大于或等于 1。

至于为什么会出现异常,原因可能很多。想到的两个是:

  • 您正在代码中的某个点在推送项目和从堆栈中弹出它们之间重新初始化/清除堆栈。

  • Assuming that, like most buttons, the method is activated by one click, you are double clicking the button, thus causing the event to fire twice, which could lead into the code performing two pop operations which is causing the issue.

于 2012-05-02T13:26:36.647 回答