专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > J2SE

pack()方法,该如何解决

发布时间:2010-06-05 05:31:00 文章来源:www.iduyao.cn 采编人员:星星草
pack()方法
在做awt相关的练习时有时用到pack()方法,想知道这个方法的作用是什么呢?什么时候要用这个方法呢?

------解决方案--------------------
关于pack()方法 
在 Frame 类中有一个从类 java.awt.Window 继承的方法 pack() 
show() 同样也继承自 java.awt.Window 

public void pack() 
调整此窗口的大小,以适合其子组件的首选大小和布局。如果该窗口和/或其所有者仍不可显示,则两者在计算首选大小之前变得可显示。在计算首选大小之后,将会验证该 Window。
------解决方案--------------------
public void pack()
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. 


意思就是说,自动调整窗口首选大小和布局,去适应该窗口的容器组建。如果这个窗口和它的容器之前没有显示出来的话,在计算首选大小之前,先显示出它们。那样的话,在首选大小被计算出来之后,就能正确显示了...
------解决方案--------------------
一般在你将控件放置在容器之后,调用这个pack()方法...


给分吧 呵呵
------解决方案--------------------
javaDoc:
调整此窗口的大小,以适合其子组件的首选大小和布局。
javaSrc:
Java code

/**
     * 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.
     * @see Component#isDisplayable
     */
    public void pack() {
    Container parent = this.parent;
    if (parent != null && parent.getPeer() == null) {
        parent.addNotify();
    }
    if (peer == null) {
        addNotify();
    }
        Dimension newSize = getPreferredSize();
        if (peer != null) {
            setClientSize(newSize.width, newSize.height);
        }

        if(beforeFirstShow) {
            isPacked = true;
        }

    validate();
    }
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: