关于java中window以及子组件的窗口居中问题
2010-07-09 18:45
369 查看
在所有的window类的子类中,都有继承自winodw类的setLocationRelativeTo方法,而这个方法是将窗体置于参数组件的中心,而我们通常使用setLocationRelativeTo(null)来使我们的窗体位于屏幕正中心,而在使用过程中我们会发现有时候我们在使用这个函数的时候窗体并不是居中的,看起来应该是以屏幕正中心的位置为左上角,为什么会造成这种结果呢?
我们创建了一个我们自己实现的基类MFrame,并且它继承自Frame这个超类,那么很好,我们在MFrame的构造函数中调用setLocationRelativeTo(null)方法,并且在应用程序显示这个组件。
看起来我们成功了,现在我们打算实现一个比较有个性的Frame,但是并不想去改变原来的MWindow这个基类,所以我们实现了一个叫做NewFrame的类,并将源程序中显示它。
我们发现这次我们又成功,但是为什么没有上边所说的问题呢?让我们看下边的代码:
这次我们发现确实位置出问题了,那么问题究竟出现在哪?其实问题出现在setSize这个函数上,如果我们想窗口在屏幕上居中,那么我需要在setLocationRelativeTo(null)方法之前调用setSize方法,这样位置才会是正确的,而我们上边代码中,在setLocationRelativeTo(null)方法之前并没有设置窗体的大小,也就是说窗体的大小为0,而调用setLocationRelativeTo(null)方法之后显然为0的窗体的location也是在屏幕中间,之后调用setSize方法,就导致了窗口以屏幕中心位置为窗口的左上点,并以800*600的窗口大小显示,所以最后并没有达到我们的要求。
正确的使用setLocationRelativeTo(null)函数的方法是,在每次需要使用这个函数之前我们应该确保已经进行了窗口大小的代码维护。
我们创建了一个我们自己实现的基类MFrame,并且它继承自Frame这个超类,那么很好,我们在MFrame的构造函数中调用setLocationRelativeTo(null)方法,并且在应用程序显示这个组件。
]import java.awt.AWTEvent; import java.awt.Frame; import java.awt.event.WindowEvent; public class Demo { public static void main(String[] args) { new MFrame(); } @SuppressWarnings("serial") public static class MFrame extends Frame { public MFrame() { super(); enableEvents(AWTEvent.WINDOW_EVENT_MASK); setTitle("This is MFrame"); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } @Override protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } }
看起来我们成功了,现在我们打算实现一个比较有个性的Frame,但是并不想去改变原来的MWindow这个基类,所以我们实现了一个叫做NewFrame的类,并将源程序中显示它。
]import java.awt.AWTEvent; import java.awt.Frame; import java.awt.event.WindowEvent; public class Demo { public static void main(String[] args) { new NewFrame(); } @SuppressWarnings("serial") public static class MFrame extends Frame { public MFrame() { super(); enableEvents(AWTEvent.WINDOW_EVENT_MASK); setTitle("This is MFrame"); setSize(800, 600); setLocationRelativeTo(null); setVisible(true); } @Override protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } @SuppressWarnings("serial") public static class NewFrame extends MFrame { public NewFrame() { super(); setTitle("This is NewFrame"); } } }
我们发现这次我们又成功,但是为什么没有上边所说的问题呢?让我们看下边的代码:
]import java.awt.AWTEvent; import java.awt.Frame; import java.awt.event.WindowEvent; public class Demo { public static void main(String[] args) { new NewFrame(); } @SuppressWarnings("serial") public static class MFrame extends Frame { public MFrame() { super(); enableEvents(AWTEvent.WINDOW_EVENT_MASK); setTitle("This is MFrame"); setLocationRelativeTo(null); } @Override protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } @SuppressWarnings("serial") public static class NewFrame extends MFrame { public NewFrame() { super(); setTitle("This is NewFrame"); setSize(800, 600); setVisible(true); } } }
这次我们发现确实位置出问题了,那么问题究竟出现在哪?其实问题出现在setSize这个函数上,如果我们想窗口在屏幕上居中,那么我需要在setLocationRelativeTo(null)方法之前调用setSize方法,这样位置才会是正确的,而我们上边代码中,在setLocationRelativeTo(null)方法之前并没有设置窗体的大小,也就是说窗体的大小为0,而调用setLocationRelativeTo(null)方法之后显然为0的窗体的location也是在屏幕中间,之后调用setSize方法,就导致了窗口以屏幕中心位置为窗口的左上点,并以800*600的窗口大小显示,所以最后并没有达到我们的要求。
正确的使用setLocationRelativeTo(null)函数的方法是,在每次需要使用这个函数之前我们应该确保已经进行了窗口大小的代码维护。
相关文章推荐
- 关于window.open弹出窗口有无标题栏的问题
- 关于java中JDK5的打包,拆包以及"=="和equals的问题
- 关于JAVA中变量类型转换以及精度问题
- 关于 android This window was lost 问题以及解决方案
- extjs4.2 -- 关于window窗口的问题
- solr中一个问题——顺便讨论java中关于中包括main函数以及涉及资源(变量)操作中的类
- 关于new、Create、对象以及窗口的一点问题
- 关于“Web 创作组件” 解决MS Office 2007找不到Office.zh-cn问题以及VS2008 SP1 安装失败需指定visualwebdeveloperww.msi所在路径
- MFC:关于MFC窗口对象(CWnd对象)与Window对象(HWND所指对象)的销毁问题
- 关于javaMail中标题以及发件人乱码的问题总结
- 关于java多个面板组件添加后,其中组件事件监听器可能没用的问题!
- java笔记:关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题(下)
- 关于java中多线程的问题的一点思考(什么情况下需要考虑到线程,以及线程安全的问题?)
- 关于window.open弹出窗口被阻止的问题
- 关于导航在浏览器中的定位以及div居中问题
- 【JAVA基础】关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
- 正泰项目问题一:【关于java中SimpleDateFormat类、Date类以及java.sql.Timestamp类之间的互相转换】
- 关于android ZXing二维码组件中java.lang.NoClassDefFoundError问题
- 关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
- java笔记:关于复杂数据存储的问题--基础篇:数组以及浅拷贝与深拷贝的问题(上)