Java 窗口位置和尺寸设置方法
在 Java 中,可以使用以下方法来定义窗口位置和尺寸:
-
setLocation(int x, int y):设置窗口的左上角位置,x 和 y 分别为横坐标和纵坐标。 -
setSize(int width, int height):设置窗口的宽度和高度,width 和 height 分别为宽度和高度的像素数。 -
setBounds(int x, int y, int width, int height):同时设置窗口的位置和尺寸,x 和 y 为左上角位置的坐标,width 和 height 为窗口的宽度和高度。
示例代码:
import javax.swing.JFrame;
public class MyFrame extends JFrame {
public MyFrame() {
// 设置窗口位置和尺寸
setLocation(100, 100); // 设置窗口左上角位置为(100, 100)
setSize(500, 500); // 设置窗口宽度和高度为500像素
// 或者使用setBounds方法同时设置位置和尺寸
// setBounds(100, 100, 500, 500);
setVisible(true); // 显示窗口
}
public static void main(String[] args) {
new MyFrame(); // 创建窗口对象
}
}
原文地址: https://www.cveoy.top/t/topic/oKhH 著作权归作者所有。请勿转载和采集!