JDialog 居中 方法二同样适合jFrame

方法一

调用JDialog的public void setLocationRelativeTo(Component c)方法,默认dialog会相对父窗口居中

方法二
自己做计算
--------------------------------------------------------------------------------------------------------------------------
DemoDialog dlg = new DemoDialog(this, "标题", true);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
dlg.setSize(400, 300);
int w = dlg.getWidth();
int h = dlg.getHeight();
dlg.setLocation( (width - w) / 2, (height - h) / 2);
dlg.setVisible(true);

你可能感兴趣的:(C++,c,C#)