How do I center a JDialog on screen?

前端 未结 5 1669
野趣味
野趣味 2020-12-07 19:38

How do I go about positioning a JDialog at the center of the screen?

5条回答
  •  执念已碎
    2020-12-07 20:18

    In Java 1.4+ you can do:

    final JDialog d = new JDialog();
    d.setSize(200,200);
    d.setLocationRelativeTo(null);
    d.setVisible(true);
    

    Or perhaps (pre 1.4):

    final JDialog d = new JDialog();
    d.setSize(200, 200);
    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    final Dimension screenSize = toolkit.getScreenSize();
    final int x = (screenSize.width - d.getWidth()) / 2;
    final int y = (screenSize.height - d.getHeight()) / 2;
    d.setLocation(x, y);
    d.setVisible(true);
    

提交回复
热议问题