我试图在我的程序中添加一个WindowListener,但是由于我的图形用户界面中没有JFrame,所以我的类扩展了JFrame,所以无法实现。有没有人知道怎么解决这个问题?
下面是我的一段代码,因为没有JFrame,所以我不能让它工作
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirmed = JOptionPane.showConfirmDialog(null,
"Are you sure you want to exit the program?", "Exit Program Message Box",
JOptionPane.YES_NO_OPTION);
if (confirmed == JOptionPane.YES_OPTION) {
dispose();
}
}
});我应该在"addWindowListener“之前写什么呢?

发布于 2015-12-03 01:59:28
如果您在JFrame扩展类中,只需将WindowListener添加到this,即该类的当前对象。看起来您是在一个匿名内部类中调用它,如果是这样,那么使用类名MyJFrameClass.this来完全限定您的this。
因此,如果您的类被命名为MyJFrameClass,那么
dispose();变成了
MyJFrameClass.this.dispose();https://stackoverflow.com/questions/34049642
复制相似问题