请问如何Swing窗口显示在屏幕的正中央? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void centerWindow(Component component) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension scmSize = toolkit.getScreenSize(); Dimension size = component.getPreferredSize(); int width = component.WIDTH, height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2), scmSize.height / 2 - (height / 2)); } Window 类:public void setLocationRelativeTo(Component c) {}使用可以简单的:win.setLocationRelativeTo(null)就行了 这个用JBuilder创建一个application时都会自动生成的。 public static void centerWindow(Component component) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension scmSize = toolkit.getScreenSize(); Dimension size = component.getPreferredSize(); int width = component.WIDTH, height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2), scmSize.height / 2 - (height / 2)); } 只要加这句代码就行了:f.setLocationRelativeTo(null); public static void centerWindow(Component component) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension scmSize = toolkit.getScreenSize(); Dimension size = component.getPreferredSize(); int width = component.WIDTH, height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2), scmSize.height / 2 - (height / 2)); } 你用一下JBuilder就知道怎么做了。 只要加这句代码就行了:f.setLocationRelativeTo(null);不是吧?就是这样就可以啦?这么简单吗? setLocationRelateiveTo(null) 这在API中那里可以找到啊? 我刚才作了一个程序测试过了,我知道JB里面是像一楼的写法,但是f.setLocationRelativeTo(null);这样的写法和那种写法,在什么时候会发生不同的变化吗?那为什么这么简单的方法不运用而去用一楼的那种JB里面自动生成的方法呢? import java.awt.*;import java.awt.event.*;public class test extends Frame{ Frame f = new Frame("测试中间"); Panel p = new Panel(); test() { Button b1 = new Button("确定"); p.add(b1); f.add(p); f.setSize(300,300); f.setLocationRelativeTo(null); //使得窗体能够显示在屏幕中间 f.show(); } public static void main(String args[]) { new test(); }}这个是我写的,也测试过了,能成功. 注意:方法setLocationRelativeTo(null);不是对所有的都适用,如对装有Image图象的ImagePanelPanel面板的窗口就不行 还是用一楼就可以 java中可变参数函数的重写 如果修改方法参数所指向的值 想做一个Panel,在Frame上能用鼠标修改大小,应该怎么做好? java新手问题 急求匹配大小写英文字母、数字、空格的正则表达式! 为什么用javamail发送邮件到30%就报错啊? 为何此程序编译出错了? 求教:关于JDBC打包成JAR的问题!! ByteArrayInputStream bais的中文问题... 如何在WINNT下安装LINUX,不重新分区可不可以? 請教關於字符的問題? float类型问题
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension scmSize = toolkit.getScreenSize();
Dimension size = component.getPreferredSize();
int width = component.WIDTH,
height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2),
scmSize.height / 2 - (height / 2));
}
public void setLocationRelativeTo(Component c) {}使用可以简单的:win.setLocationRelativeTo(null)就行了
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension scmSize = toolkit.getScreenSize();
Dimension size = component.getPreferredSize();
int width = component.WIDTH,
height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2),
scmSize.height / 2 - (height / 2));
}
f.setLocationRelativeTo(null);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension scmSize = toolkit.getScreenSize();
Dimension size = component.getPreferredSize();
int width = component.WIDTH,
height = component.HEIGHT; component.setLocation(scmSize.width / 2 - (width / 2),
scmSize.height / 2 - (height / 2));
}
f.setLocationRelativeTo(null);
不是吧?就是这样就可以啦?这么简单吗? setLocationRelateiveTo(null) 这在API中那里可以找到啊?
这样的写法和那种写法,在什么时候会发生不同的变化吗?那为什么这么简单的方法不运用而去用一楼的那种JB里面自动生成的方法呢?
import java.awt.event.*;public class test extends Frame
{
Frame f = new Frame("测试中间");
Panel p = new Panel();
test()
{
Button b1 = new Button("确定");
p.add(b1);
f.add(p);
f.setSize(300,300);
f.setLocationRelativeTo(null); //使得窗体能够显示在屏幕中间
f.show();
}
public static void main(String args[])
{
new test();
}
}这个是我写的,也测试过了,能成功.
方法setLocationRelativeTo(null);
不是对所有的都适用,
如对装有Image图象的ImagePanelPanel面板的窗口就不行
还是用一楼就可以