请问如何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面板的窗口就不行 还是用一楼就可以 循环求值问题 小白一枚,请教关于包装类中字符串类型转换成基本数据类型的问题 求sqljdbc.jar 下载地址 输出1,2,2,3,4,5的所有排列组合,4不能在第三位,3和5不能相邻 如何即时获取QQ聊天信息 追踪 JAVA 程序的内存使用量 如何使用cloudscape数据库? 请问如何生成设置IP的文本框,就像windows下的一样,textField中有三个点! javamail里面的中文问题(接收邮件,发送的时候都已经解决) 请教一个Java接口的理解问题 請教關於字符的問題? 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面板的窗口就不行
还是用一楼就可以