
使用getWidth()方法即可获取窗口的宽度。
import javax.swing.JFrame;
public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My Frame");
        frame.setSize(500, 300);
        int width = frame.getWidth();
        System.out.println("窗口宽度:" + width);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}执行上述代码后,控制台输出的结果将为:窗口宽度:500,即窗口的宽度为500。