import java.awt.*;
import java.awt.event.*;public class layoutflow extends Frame
{
layoutflow()
{
super("Frame with FlowLayout");
setLayout(new FlowLayout());
setBackground(Color.green);
setVisible(true);
add(new Button("button 01"));
add(new Button("button 02"));
add(new Button("button 03"));
add(new Button("button 04"));
add(new Button("button 05"));
pack();
addWindowListener(
new WindowAdapter(){
public void WindowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});
}
public static void main(String args[])
{
layoutflow frm=new layoutflow();
}
} 为什么这个程序无法退出,即点击“×”无法关闭程序
import java.awt.event.*;public class layoutflow extends Frame
{
layoutflow()
{
super("Frame with FlowLayout");
setLayout(new FlowLayout());
setBackground(Color.green);
setVisible(true);
add(new Button("button 01"));
add(new Button("button 02"));
add(new Button("button 03"));
add(new Button("button 04"));
add(new Button("button 05"));
pack();
addWindowListener(
new WindowAdapter(){
public void WindowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});
}
public static void main(String args[])
{
layoutflow frm=new layoutflow();
}
} 为什么这个程序无法退出,即点击“×”无法关闭程序
------------------------------------------------------
加这个,是关闭程序.因为JAVA窗体默认关闭是隐藏窗口,没有真正退出程序.
new WindowAdapter(){
public void windowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});是大小写问题.是windowClosing方法首字母小写.类名首字母大写.
addWindowListener(
new WindowAdapter(){
public void WindowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});中的WindowClosing()方法错误,应该是小写字母开头.
addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent e){
setVisible(false);
System.exit(0);}});
这样就没问题了,另外建议类名首字母大写.