First:
Causes this window to be sized to fit
Preferred size and layouts of its subcomponents. If
Window and/or its owner are not yet displayable, both
Are Made displayable before calculating the preferred
Size. The window will be validated after
Preferredsize is calculated.
This is the definition of Pack () in Java API. I don't know where your instructions are obtained.
From the above description, we can understand that pack () is used to automatically adjust the size of a window object. The goal of the adjustment is to make it suitable for him and all its components.
(Of course, this range must be calculated first. If the size of some space cannot be calculated, this method is invalid. However, if it cannot be calculated,
So how to display it ?)
The following is a simple example of using pack (). You can refer to the following. If the Pack () method is used and the Controller size and range of the window are used, for example, setsize will be blocked.
Import java. AWT. gridlayout;
Import javax. Swing. jbutton;
Import javax. Swing. jdialog;
Import javax. Swing. jlist;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. scrollpanelayout;
/**
* Try to use the method "pack ()"
* @ Author liym
*/
Public class jdialogtest extends jdialog {
/* Component */
Private jbutton = NULL;
/**
* Construct Method
* @ Author liym
*/
Public jdialogtest (){
// Initial Method
Init ();
// Try to use the method
This. Pack ();
}
/**
* Initial Method
* @ Return <code> void <code>
*/
Private void Init (){
// Set the size of the dialog
This. setsize (200,400 );
// Set the layout of the dialog
This. getcontentpane (). setlayout (New gridlayout ());
// The component in the dialog
Jbutton = new jbutton ("button ");
This. getcontentpane (). Add (jbutton );
}
/**
* Main method.
*
* @ Param ARGs string [] inputed strings
* @ Return <code> void </code>
*/
Public static void main (string [] ARGs ){
Jdialogtest = new jdialogtest ();
Jdialogtest. setvisible (true );
}
}