2013年10月22日星期二

[ Ask ] about the interface program main function problems

I was in a Tetris game program see the following piece of code in the process a little bit on the main question:
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 (" game" ) ;
JMenuItem newgame = game.add (" New Game" ) ;
JMenu help = new JMenu (" Help " ) ;
JMenuItem about = help.add (" on" ) ;
menu.add (game);
menu.add (help);
frame.setLocationRelativeTo (null);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (440, 550);
frame.setTitle ("Tetris beta version " ) ;
/ / frame.setUndecorated (true);
frame.setVisible (true);
frame.setResizable (false);
}
}

My question is, in the main , after creating the frame , main function soon quit, then the program is not also quit it ? But evidently the program does not exit , then the main function exits mean anyway ?
------ Solution ---------------------------------------- ----

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Here is explained in the document
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".

This sentence EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method.
exit method calls the system default , when you point the Close button when it exits the main function , otherwise the program will have been in operation, do not know if my interpretation is wrong, see more of the document inside the explanation, you will will better understand how matter

------ Solution ------------------------------------ --------
a program, usually has at least one thread, which is the main thread, which is the background thread is a daemon thread , the other end of the thread before the end .
------ Solution ---------------------------------------- ----

understand it bears stickers Oh
------ For reference only --------------------- ------------------


" When you click the Close button to exit the main function when it is " still do not understand that when executed statement frame.setResizable (false) after , main function exits.
------ For reference only -------------------------------------- -

  
" When you click the Close button to exit the main function when it is " still do not understand that when executed statement frame.setResizable (false) after , main function exits.  

About setResizable document also has detailed instructions
public void setResizable (boolean resizable)
Sets whether this frame is resizable by the user.

Parameters:
resizable - true if this frame is resizable; false otherwise.

execute statement frame.setResizable (false) does not exit the program, execution sentence statement only makes the window size is fixed. Do you think this is the end of the process a statement , as long as the implementation language program in relation to the sentence out , is not it ?
------ For reference only -------------------------------------- -

    
" When you click the Close button to exit the main function when it is " still do not understand that when executed statement frame.setResizable (false) after , main function exits.          
  
About setResizable document also has detailed instructions   
public void setResizable (boolean resizable)   
Sets whether this frame is resizable by the user.   
  
Parameters:   
resizable - true if this frame is resizable; false otherwise.   
  
execute statement frame.setResizable (false) does not exit the program, execution sentence statement only makes the window size is fixed. Do you think this is the end of the process a statement , as long as the implementation language program in relation to the sentence out , is not it ?  

executed frame.setResizable (false) after at least main function should be to quit. I do not know do not exit the program exits .
------ For reference only -------------------------------------- -
program is certainly no exit . frame these windows is the new child thread , you main end. But the child thread does not end so you can see the interface is still there.
------ For reference only -------------------------------------- -
and the following program the same reason.

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
..

------ For reference only ----------------------------------- ----


So, is only when all the threads ( the main thread , the child thread ) have exited , the program just quit it ? If I put a child thread forget quit, what would happen .
------ For reference only -------------------------------------- -

is there any function call can end all the threads and let the program exit ? There exit () function do ?
------ For reference only -------------------------------------- -

is there any function call can end all the threads and let the program exit ? There exit () function do ?  

in your function is executed to the end , do System.exit (0);
whole procedure is finished
------ For reference only ----------------------------- ----------
Thank you for your answer , I understand.

没有评论:

发表评论