下面这段代码在控制台运行之后,就关不了了,求高手赐教一个关闭的方法,我刚学AWT类,有资料的大神请赐教,谢谢了。。package cn.puruidong.Test;
import java.awt.*;
public class Gui{
private Frame f;
private Button b1;
private Button b2;
public static void main(String args[]){ Gui that = new Gui();
that.go();
}
public void go(){
f = new Frame("GUI example");
f.setLayout(new FlowLayout());
//设置布局管理器为FlowLayout
b1 = new Button("Press Me"); //按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
}
}
import java.awt.*;
public class Gui{
private Frame f;
private Button b1;
private Button b2;
public static void main(String args[]){ Gui that = new Gui();
that.go();
}
public void go(){
f = new Frame("GUI example");
f.setLayout(new FlowLayout());
//设置布局管理器为FlowLayout
b1 = new Button("Press Me"); //按钮上显示字符"Press Me"
b2 = new Button("Don't Press Me");
f.add(b1);
f.add(b2);
f.pack();//紧凑排列,其作用相当于setSize(),即让窗口尽量小,小到刚刚能够包容住b1、b2两个按钮
f.setVisible(true);
}
}
我建议还是用swing,比awt好多了,而且方法用起来也方便,一个setDeaultCloseOperation(true)就可以关闭窗口了
f.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
IDE用多了吧。
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);