方法一
调用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);
JDialog 居中 方法二同样适合jFrame
最新推荐文章于 2024-01-18 13:51:04 发布