java中setSize(),setLocation()和setBounds()的关系

来源:互联网 发布:耐克鞋淘宝网 编辑:程序博客网 时间:2024/06/07 01:15

之前也一直没有太在意这三个函数之间的关系,今天遇到了,就特地查了下:


setSize(int width, int height):其实就是定义控件的大小,有两个参数,分别对应宽度和高度;

setLocation(int x, int y):将组件移到新位置,用x 和 y 参数来指定新位置的左上角

setBounds(int x, int y, int width, int height):四个参数,既定义组件的位置,也定义控件的大小; 其实它就是上面两个函数的功能的组合


然后看源码也可以有所发现:

我们发现 确实setSize()和setLocation()是通过调用setBounds实现的

 public void setSize(int width, int height) {        resize(width, height);    }    /**     * @deprecated As of JDK version 1.1,     * replaced by <code>setSize(int, int)</code>.     */    @Deprecated    public void resize(int width, int height) {        synchronized(getTreeLock()) {            setBoundsOp(ComponentPeer.SET_SIZE);            setBounds(x, y, width, height);        }    }



 public void setLocation(int x, int y) {        move(x, y);    }    /**     * @deprecated As of JDK version 1.1,     * replaced by <code>setLocation(int, int)</code>.     */    @Deprecated    public void move(int x, int y) {        synchronized(getTreeLock()) {            setBoundsOp(ComponentPeer.SET_LOCATION);            setBounds(x, y, width, height);        }    }




1 0
原创粉丝点击