//使对话框居中的函数,不会因为分辨率不同而位置不同
public static void setLocation(JDialog dialog) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = dialog.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
dialog.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
//要想调用此函数,只要在Dialog中使用
JBGlobal.setLocation(this);
//JBGlobal是全局类,还要注意的是上面的语句要在
this.setSize(new Dimension(500, 395));
//后面,否则对话框不会居中。
public static void setLocation(JDialog dialog) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = dialog.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
dialog.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
//要想调用此函数,只要在Dialog中使用
JBGlobal.setLocation(this);
//JBGlobal是全局类,还要注意的是上面的语句要在
this.setSize(new Dimension(500, 395));
//后面,否则对话框不会居中。
博客给出了一个使对话框居中的Java函数setLocation,该函数不会因分辨率不同而使对话框位置改变。还说明了调用此函数的方法,需在Dialog中使用JBGlobal.setLocation(this),且该语句要在设置对话框大小语句之后,否则对话框无法居中。
1053

被折叠的 条评论
为什么被折叠?



