在所有的window类的子类中,都有继承自winodw类的setLocationRelativeTo方法,而这个方法是将窗体置于参数组件的中心,而我们通常使用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)函数的方法是,在每次需要使用这个函数之前我们应该确保已经进行了窗口大小的代码维护。
我们创建了一个我们自己实现的基类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)函数的方法是,在每次需要使用这个函数之前我们应该确保已经进行了窗口大小的代码维护。