为什么main函数跑完了,程序没结束?
import java.awt.*;
import java.awt.event.*;
public class Form extends WindowAdapter
{
    Frame f;
 
    public void display()
    {
        f = new Frame("Form");
        f.setSize(200,200);
        f.setLocation(200,140);
        f.setBackground(Color.lightGray);
   
        f.addWindowListener(new WinClose());
        f.setVisible(true);
        System.out.println("display is over");
    }
 
    public static void main(String[] args)
    {
        (new Form()).display();
        System.out.println("main is over"); //为什么main函数跑完了,程序没结束?
    }
}class WinClose extends WindowAdapter
{
    public  void windowClosing(WindowEvent e)
{
        System.exit(0);
    }

解决方案 »

  1.   

    因为awt里有很多线程在跑,还要进行用户输入监听等工作,Main结束不代表这些线程也会结束,所以不会使整个程序结束。
      

  2.   

    首先你没关闭窗体,程序当然不会结束;再说你不设置setDefaultCloseOperation(int Operation)方法的话,即使你关闭窗体程序也不会结束。不是说主函数运行完了程序就关闭了,那你打开那么多应用程序的时候是你打开了然后应用程序就结束了吗?比如开qq你开开qq主函数运行完了,程序不还是在进行吗,因为你主函数中又运行了其他的进程,比如你这个程序中的窗体相当于你开的进程,进程没关闭程序怎么可能结束