java图形界面swing:如何让窗口显示在显示屏中间

setLocationRelativeTo(Component c);这个函数可以将打开的窗口设置为居中显示,代码如下:

public class f extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					f frame = new f();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public f() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setLocationRelativeTo(contentPane);//窗口居中显示;
		setContentPane(contentPane);
	}

猜你喜欢

转载自blog.csdn.net/qq_40572277/article/details/84647879