java中setSize(),setLocation(),setBounds三者之间的关系和用法

来源:互联网 发布:linux 休眠 编辑:程序博客网 时间:2024/06/11 04:35

开始学习java,感觉有好多小的东西都需要去进行总结,唉,没办法,既然选择了java那就一步一步来吧!加油!



setSize(int width,int heigth):

就是定义控件的大小,setSize里面有两个参数,分别对应该控件的宽度和高度.

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);         }     }  

学java不久,以后还会多多发表一些东西,希望在此同时能够得到大佬们的指教。



阅读全文
1 0