首先:
Causes this Window to be sized to fit the
preferred size and layouts of its subcomponents. If the
window and/or its owner are not yet displayable, both
are made displayable before calculating the preferred
size. The Window will be validated after the
preferredSize is calculated.
这个是Java API对pack()的定义,不知道你的说明是那里得到的
那么从上面的说明可以理解到,pack()的用处就是将一个窗口对象的大小进行自动调整,调整的目标就是让它适合为他以及他其中的所有component
的最小范围,(“当然这个范围是要首先计算出来的,如果因为某些空间的大小无法计算出来的话,这个方法是无效的”我们头的原文。不过如果计算不出来的话,
那么怎么去显示阿?)
以下是一个简单的pack()的使用例子,可以参照,如果使用了pack()的方法,关于这个window的控制器大小和范围的方法,如setSize都将被屏蔽
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 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 jDialogTest = new JDialogTest();
jDialogTest.setVisible(true);
}
}
swt中 pack()
最新推荐文章于 2023-12-25 22:53:14 发布