1,pack()的作用是什么?
doc中写:“Causes the receiver to be resized to its preferred size”
例如:Shell shell=new Shell();
shell.pack();
那么shell就是receiver吧,那么to be resized to its preferred size又是什么意思啊?
2,SWT.TOOL的作用是什么?
3,如果一个Shell建立在另一个Shell上,它们之间是什么关系呢?谢谢大家!
doc中写:“Causes the receiver to be resized to its preferred size”
例如:Shell shell=new Shell();
shell.pack();
那么shell就是receiver吧,那么to be resized to its preferred size又是什么意思啊?
2,SWT.TOOL的作用是什么?
3,如果一个Shell建立在另一个Shell上,它们之间是什么关系呢?谢谢大家!
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);
}}
Causes the receiver to be resized to its preferred size. For a composite, this involves computing the preferred size from its layout, if there is one.
现在我明白了,谢谢。