我在一个俄罗斯方块游戏程序中看到如下一段代码,对main中的流程有点疑问:
public class Tetris extends JFrame {
    public static void main(String[] args) {
        Tetris frame = new Tetris();
        JMenuBar menu = new JMenuBar();
        frame.setJMenuBar(menu);
        JMenu game = new JMenu("游戏");
        JMenuItem newgame = game.add("新游戏");
        JMenu help = new JMenu("帮助");
        JMenuItem about = help.add("关于");
        menu.add(game);
        menu.add(help);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(440, 550);
        frame.setTitle("Tetris内测版");
        // frame.setUndecorated(true);
        frame.setVisible(true);
        frame.setResizable(false);
    }
}我的疑问是,在main中,创建完frame后,main函数很快就退出了,那么程序是不是也会退出呢?但是看样子程序并没有退出,那么main函数退出代表什么意思呢?

解决方案 »

  1.   


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    下面是文档中的解释
    setDefaultCloseOperation
    public void setDefaultCloseOperation(int operation)
    Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices: DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object. 
    HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects. 
    DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects. 
    EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications. 
    The value is set to HIDE_ON_CLOSE by default. Changes to the value of this property cause the firing of a property change event, with property name "defaultCloseOperation". 这句话EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method.
    调用系统默认的exit方法,当你点关闭按钮的时候才退出main函数,否则程序就会一直都在运行,不知道我的解释对不对,多看看文档里面的解释,你就会更加明白怎么回事了
      

  2.   


    “当你点关闭按钮的时候才退出main函数”,还是不明白,当执行完语句frame.setResizable(false)后,main函数就退出了。
      

  3.   


    “当你点关闭按钮的时候才退出main函数”,还是不明白,当执行完语句frame.setResizable(false)后,main函数就退出了。关于setResizable文档中也有详细的说明
    public void setResizable(boolean resizable)
    Sets whether this frame is resizable by the user. Parameters:
    resizable - true if this frame is resizable; false otherwise.执行语句frame.setResizable(false)并不会退出程序,执行这句语句只是使得窗口的大小是固定的。你是不是认为这是程序的最后一句语句,只要执行到这句语言程序就该退出,是么?
      

  4.   


    “当你点关闭按钮的时候才退出main函数”,还是不明白,当执行完语句frame.setResizable(false)后,main函数就退出了。关于setResizable文档中也有详细的说明
    public void setResizable(boolean resizable)
    Sets whether this frame is resizable by the user. Parameters:
    resizable - true if this frame is resizable; false otherwise.执行语句frame.setResizable(false)并不会退出程序,执行这句语句只是使得窗口的大小是固定的。你是不是认为这是程序的最后一句语句,只要执行到这句语言程序就该退出,是么?执行完frame.setResizable(false)后,至少main函数应该是退出了。程序退出不退出不知道。
      

  5.   

    程序肯定是没有退出的。frame这些个窗口是新的子线程,你main结束了。但是子线程没有结束所以你看到界面还在。
      

  6.   

    和下面的程序一样的道理。public class Demo implements Runnable{

    private String name;

    public Demo(){

    }
    public Demo(String name){
    this.name=name;
    }
    public void run(){
    for(int i=0;i<20;i++){
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(name+" 运行 "+i);
    }
    } public static void main(String[] args) {
    // TODO Auto-generated method stub
    Demo demo1=new Demo("线程A");
    Demo demo2=new Demo("线程B"); Thread thread1=new Thread(demo1);
    Thread thread2=new Thread(demo2);
    thread1.start();
    thread2.start();
    System.out.println(111111);
    }}打印
    111111
    线程B 运行 0
    线程A 运行 0
    线程A 运行 1
    线程B 运行 1
    线程B 运行 2
    线程A 运行 2
    线程B 运行 3
    线程A 运行 3
    线程B 运行 4
    线程A 运行 4
    线程B 运行 5
    线程A 运行 5
    线程B 运行 6
    线程A 运行 6
    线程B 运行 7
    线程A 运行 7
    线程A 运行 8
    线程B 运行 8
    ..
      

  7.   

    一个程序中,一般都有至少一个线程,这个线程就是main线程,它是后台线程,也是守护线程,在其他线程结束后,才结束。
      

  8.   

    有没有什么函数调用后可以结束所有线程并让程序退出的?有exit()函数吗?
      

  9.   

    有没有什么函数调用后可以结束所有线程并让程序退出的?有exit()函数吗?在你函数执行到最后,执行System.exit(0);
    整个程序就结束了