上传者: yll_xyy
|
上传时间: 2022-05-23 16:40:17
|
文件大小: 2KB
|
文件类型: JAVA
public class ShowCenter {
/**
* 在中间位置显示窗体
* @param frame 要显示的窗体
*/
public static void centerShowJFrame(JFrame frame) {
//定义工具包:Toolkit.getDefaultToolkit()
//获取屏幕的尺寸 Dimension screenSize
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();//窗口的尺寸
//获得窗口高frameSize.height
//获取屏幕的高screenSize.height
//如果窗口的高度大于屏幕的高度,则以屏幕的高度为准
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
//获得窗口宽frameSize.width
//获取屏幕的宽screenSize.width
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
//设置中间位置
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
//设置窗口可见
frame.setVisible(true);
}
public static void centerShowJFrame(JDialog dialog) {
//定义工具包:Toolkit.getDefaultToolkit()
//获取屏幕的尺寸 Dimension screenSize
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = dialog.getSize();//窗口的尺寸
//获得窗口高frameSize.height
//获取屏幕的高screenSize.height
//如果窗口的高度大于屏幕的高度,则以屏幕的高度为准
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
//获得窗口宽frameSize.width
//获取屏幕的宽screenSize.width
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
//设置中间位置
dialog.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
//设置窗口可见
dialog.setVisible(true);
}
}