swing 窗口设置居中的代码如下:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Application{
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("测试");
frame.setSize(500, 350);// 宽度、高度
frame.setLocationRelativeTo(null); // 窗口居中
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}