当前位置: 技术问答>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);
}
}
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。