当前位置: 技术问答>java相关
任何使窗口居中显示。
来源: 互联网 发布时间:2015-11-06
本文导语: 通过什么方法,可以获得环境变量,是窗口居中显示。 | 调用如下CenterShow class即可: 比如在一个扩展Frame class的jbInit()方法内调用如下 CenterShow cs = new CenterShow(this); 附CenterShow代码: import...
通过什么方法,可以获得环境变量,是窗口居中显示。
|
调用如下CenterShow class即可:
比如在一个扩展Frame class的jbInit()方法内调用如下
CenterShow cs = new CenterShow(this);
附CenterShow代码:
import java.awt.*;
public class CenterShow {
public CenterShow(Frame frame) {//目标显示为界面Frame/JFrame
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
public CenterShow(Dialog dialog) {//目标显示为对话框Dialog/JDialog
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - dialogSize.width) / 2,
(screenSize.height - dialogSize.height) / 2);
}
}
比如在一个扩展Frame class的jbInit()方法内调用如下
CenterShow cs = new CenterShow(this);
附CenterShow代码:
import java.awt.*;
public class CenterShow {
public CenterShow(Frame frame) {//目标显示为界面Frame/JFrame
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
public CenterShow(Dialog dialog) {//目标显示为对话框Dialog/JDialog
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dialogSize = dialog.getSize();
if (dialogSize.height > screenSize.height) {
dialogSize.height = screenSize.height;
}
if (dialogSize.width > screenSize.width) {
dialogSize.width = screenSize.width;
}
dialog.setLocation((screenSize.width - dialogSize.width) / 2,
(screenSize.height - dialogSize.height) / 2);
}
}