相当简单........this.setLocationRelativeTo(null);原来的方法去获取屏幕坐标啊 自己计算啊 什么的 随意间搜了搜 就上面那一句话搞定哦 擦.......散散分分
解决方案 »
- 关于JDBC连接sql server2005的问题
- java socket 通信
- 求高手
- 连接oracle的一个留言版的一个页面,要连sqlserver2005,怎么改
- 用正则怎么表示这个字串<meta http-equiv="content-type" content="text/html; charset=utf-8">
- 你希望的Java 7能有哪些改变呢????
- 新手求教:SWT中如何实现undo/redo
- javac编译通过运行时 Exception in thread "main" java.lang.NoClassDefFoundError: SqlService
- 调查一下:请问大家一般是怎么来看(学习)<thinking in java>这本书的?
- 如何正确进行数据库操作
- java反编译工具JD-GUI下载
- 在线求解,(统计字符串中的英文字母数量,并替换成下划线)
swing 过时了。。
我现在用的就是 SWT 感觉还不错
还有,现在都说SWT/JFace很火,有时间研究一下
public class Test extends JFrame{
/*
*获得屏幕中央位置:
* */
public Dimension getcenter(){
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
Dimension dim=new Dimension((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
return dim;
}//在构造方法中调用一下就可以了:
public Test(){
//设置中央位置显示:
setSize(830,600);
setLocation(this.getcenter().width,this.getcenter().height);
}}
楼主你的方法好像不管用啊,求详解!!public class Test extends JFrame { /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame
*/
public Test() {
super();
//好像不管用
setLocationRelativeTo(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
}}
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
Dimension dim=new Dimension((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
return dim
通过计算坐标就可设置出居中的界面
我在Ubuntu/GNOME 下经常遇到
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
Dimension dim=new Dimension((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2);
return dim
[code=Jav]public static void showWindowToCenter(Window window) {
// Center the window
window.pack();
showWindowToCenter(window, true);
} public static void showWindowToCenter(Window window, boolean needpack) {
// Center the window
if (needpack) window.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = window.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
window.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); window.setVisible(true);
}[/code]
[code=Jav]public static void showWindowToCenter(Window window) {
// Center the window
window.pack();
showWindowToCenter(window, true);
} public static void showWindowToCenter(Window window, boolean needpack) {
// Center the window
if (needpack) window.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = window.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
window.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); window.setVisible(true);
}[/code]
public static void showWindowToCenter(Window window) {
// Center the window
window.pack();
showWindowToCenter(window, true);
} public static void showWindowToCenter(Window window, boolean needpack) {
// Center the window
if (needpack) window.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = window.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
window.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); window.setVisible(true);
}
你肯定是先setLocationRelativeTo(null);
然后再setSize();的吧应该先setSize();
之后再setLocationRelativeTo(null);
MainFrame frm = new MainFrame();
//居中显示
int w = (Toolkit.getDefaultToolkit().getScreenSize().width - frm.getWidth()) / 2;
int h = (Toolkit.getDefaultToolkit().getScreenSize().height - frm.getHeight()) / 2;
frm.setLocation(w, h); //悲剧的以前一直这么用
public void setLocationRelativeTo(Component c)设置窗口相对于指定组件的位置。
如果组件当前未显示,或者 c 为 null,则此窗口将置于屏幕的中央。中点可以使用 GraphicsEnvironment.getCenterPoint 确定。 如果该组件的底部在屏幕外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则 Window 将被放置在左部,反之亦然。
参数:
c - 确定窗口位置涉及的组件
从以下版本开始:
1.4
另请参见:
GraphicsEnvironment.getCenterPoint()
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=this.getSize();
Dimension dim=new Dimension((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
return dim
}
获取屏幕的高度、宽度,窗口的高度、宽度,相减 除以 2,再setLocation就哦了,
首先 哥鄙视你...
我整C#的,偶然间有个Java项目就做了...
我说出来是为了让更多不知道的人知道...
这叫经验共享...
再次 哥再鄙视你...
最后 哥还是鄙视你...
行!!!
int h = (Toolkit.getDefaultToolkit().getScreenSize().height - frm.getHeight()) / 2;
frm.setLocation(w, h);