【发布时间】:2012-03-20 05:20:19
【问题描述】:
当用户单击关闭按钮或另一个显示退出的 JButton 时,我必须将程序的状态保存在 xml 中。我有它,所以它在窗口关闭时使用 WindowListener 运行它运行它。但这仅在您单击红色 X 关闭按钮时才有效,但在您有另一个按钮时无效。我可以在 actionListener 中为按钮执行 System.exit(0) 之前运行它,但是我需要的一些类的信息我无法从那个地方访问。
这是给框架的。
frame.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent arg0){}
public void windowClosed(WindowEvent arg0){}
public void windowClosing(WindowEvent arg0)
{
myScreen.write.writeToXmlFile(myScreen.pics, myScreen.row, myScreen.col);
}
public void windowDeactivated(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowOpened(WindowEvent arg0){}
});
这是按钮中的代码。
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
我无法从包含按钮的类访问 myScreen。
【问题讨论】:
标签: java swing jframe jbutton windowlistener