当前位置: 代码迷 >> 综合 >> java中setSize(),setLocation(),setBounds三者之间的关系和用法
  详细解决方案

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

热度:91   发布时间:2024-01-16 20:13:52.0

开始学习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不久,以后还会多多发表一些东西,希望在此同时能够得到大佬们的指教。