this.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
if(jtf.getText()==null||jtf.getText().trim().equals("")){
System.exit(0);
}
try {
if(sbuff!=null){
File f=new File(jtf.getText().substring(7)+".txt");
FileOutputStream fos=new FileOutputStream(f);
String s=sbuff.toString();
byte[] b=s.getBytes();
if(b!=null)
fos.write(b);
fos.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
我用了匿名类,你还可以去实现WindowListener接口,去实现里面的方法就行了