【发布时间】:2014-04-06 10:24:08
【问题描述】:
我正在尝试在窗口(和脚本)关闭之前保存信息(将矢量保存到文件中)。我到处检查和搜索我找不到做什么。
我遇到的错误是
未报告的异常 java.lang.exception;必须被抓住或申报 被抛出 savePlayers()。
但是,我使用的是 loadPlayers,它的作用正好相反,我对异常没有任何问题。请帮助任何人?代码是:
static public void savePlayers() throws Exception
{
//serialize the List
try
{
FileOutputStream file = new FileOutputStream(FILE_NAME);
ObjectOutputStream output = new ObjectOutputStream(file);
output.writeObject(players);
output.close();
}
catch(IOException ex)
{
System.out.println (ex.toString());
}
}
public static void main (String[] args) throws Exception
{
JFrame frame = new JFrame("Teams");
frame.setSize(700,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
try
{
savePlayers();
}
catch (IOException ex) {
ex.printStackTrace();
}
System.exit(0);
}
});
【问题讨论】:
-
你为什么使用
System.exit(0)?你觉得frame.dispose()怎么样? -
@Braj 我也退出了应用程序,所以不确定 dispose 是否这样做
-
阅读
frame.dispose()方法。
标签: java swing exception file-io windowlistener