我要加一个关闭窗口的
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
这段要插在下面的哪里?还要改哪里?
import java.awt.*;
class FrmApp
{
static Frame fra=new Frame("窗口");
public static void main(String args[])
{
fra.setSize(250,150);
fra.setLocation(100,200);
fra.setVisible(true);
System.out.println("State:"+fra.getState());
System.out.println("Title:"+fra.getTitle());
System.out.println("Visible:"+fra.isVisible());
}
}
addWindowListener是Frame类继承下来的方法,直接在对象中添加,我帮你把代码写完整了。
=========================================================================
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
class FrmApp
{
static Frame fra=new Frame("窗口");
public static void main(String args[])
{
fra.setSize(250,150);
fra.setLocation(100,200);
fra.setVisible(true);
fra.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
System.out.println("State:"+fra.getState());
System.out.println("Title:"+fra.getTitle());
System.out.println("Visible:"+fra.isVisible());
}
import java.awt.*;
class FrmApp
{
public FrmApp()
{
static Frame fra=new Frame("窗口");
fra.setSize(250,150);
fra.setLocation(100,200);
fra.setVisible(true);
System.out.println("State:"+fra.getState());
System.out.println("Title:"+fra.getTitle());
System.out.println("Visible:"+fra.isVisible());
fra.addWindowListener(new WinClose());
}
public static void main(String args[])
{
new FrmApp();
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e);
{
System.exit(0);
}
public void windowOpened(WindowEvent e);{}
public void windowActivated(WindowEvent e);{}
public void windowDeactivated(WindowEvent e);{}
public void windowClosed(WindowEvent e);{}
public void windowIconified(WindowEvent e);{}
public void windowDeiconified(WindowEvent e);{}
}
}
在 【System.out.println("Visible:"+fra.isVisible());】的前面写frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);