当前位置: 技术问答>java相关
怎么让JFrame居中显示???
来源: 互联网 发布时间:2015-09-07
本文导语: JFrame居中显示 | /*让窗口或控件(Component)处于屏幕中央*/ public static void centerWindow(Component component) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension comp...
JFrame居中显示
|
/*让窗口或控件(Component)处于屏幕中央*/
public static void centerWindow(Component component)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension componentSize = component.getSize();
if (componentSize.height > screenSize.height) {
componentSize.height = screenSize.height;
}
if (componentSize.width > screenSize.width) {
componentSize.width = screenSize.width;
}
component.setLocation((screenSize.width - componentSize.width) / 2, (screenSize.height - componentSize.height) / 2);
component.setVisible(true);
}
public static void centerWindow(Component component)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension componentSize = component.getSize();
if (componentSize.height > screenSize.height) {
componentSize.height = screenSize.height;
}
if (componentSize.width > screenSize.width) {
componentSize.width = screenSize.width;
}
component.setLocation((screenSize.width - componentSize.width) / 2, (screenSize.height - componentSize.height) / 2);
component.setVisible(true);
}