Java中的setBounds()方法有什么用?

What is the use of setBounds() method in Java?

setBounds()

setBounds()方法需要四个参数。 前两个参数是左上角的x和y坐标
组件的角,第三个参数是组件的宽度,第四个参数是组件的高度。

句法

1
setBounds(int x-coordinate, int y-coordinate, int width, int height)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import javax.swing.*;
import java.awt.*;
public class SetBoundsTest {
 public static void main(String arg[]) {
   JFrame frame = new JFrame("SetBounds Method Test");
   frame.setSize(375, 250);
   // Setting layout as null
   frame.setLayout(null);
   // Creating Button
   JButton button = new JButton("Hello Java");
   // Setting position and size of a button
   button.setBounds(80,30,120,40);
   frame.add(button);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
 }
}

输出量