简体   繁体   English

如何最好地定位 Swing GUI?

[英]How to best position Swing GUIs?

In another thread I stated that I liked to center my GUIs by doing something like this:另一个线程中,我说我喜欢通过做这样的事情来使我的 GUI 居中:

JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new HexagonGrid());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

But Andrew Thompson had a different opinion, to instead call但是安德鲁汤普森有不同的意见,而是打电话

frame.pack();
frame.setLocationByPlatform(true);

and inquiring minds want to know why?好奇的头脑想知道为什么?

To my eye, a GUI in the middle of the screen looks so.. "splash-screen'ish".在我看来,屏幕中间的 GUI 看起来很……“闪屏”。 I keep waiting for them to disappear and the real GUI to appear!我一直在等待它们消失并等待真正的GUI 出现!

Since Java 1.5 we've had access to Window.setLocationByPlatform(boolean) .从 Java 1.5 开始,我们可以访问Window.setLocationByPlatform(boolean) which..哪个..

Sets whether this Window should appear at the default location for the native windowing system or at the current location (returned by getLocation) the next time the Window is made visible.设置此 Window 应出现在本机窗口系统的默认位置,还是在下一次使 Window 可见时出现在当前位置(由 getLocation 返回)。 This behavior resembles a native window shown without programmatically setting its location.此行为类似于未以编程方式设置其位置的本机窗口。 Most windowing systems cascade windows if their locations are not explicitly set.大多数窗口系统级联窗口,如果它们的位置没有明确设置。 The actual location is determined once the window is shown on the screen.一旦窗口显示在屏幕上,实际位置就确定了。

Have a look at the effect of this example that puts 3 GUIs into the default positions as chosen by the OS - on Windows 7, Linux with Gnome & Mac OS X.看看这个例子的效果,它将 3 个 GUI 放入操作系统选择的默认位置 - 在 Windows 7、Linux 和 Gnome 和 Mac OS X 上。

Windows 7 上的堆叠窗口在此处输入图片说明Mac OS X 上的堆叠窗口

(3 lots of) 3 GUIs neatly stacked. (3 批)3 个 GUI 整齐堆叠。 This represents 'the path of least surprise' for the end user, since it is how the OS might position 3 instances of the default plain-text editor (or anything else, for that matter).这代表了最终用户的“最不意外的路径”,因为它是操作系统如何定位默认纯文本编辑器(或其他任何东西)的 3 个实例的方式。 My thanks to trashgod for the Linux & Mac.感谢 Linux 和 Mac 的垃圾神。 images.图片。

Here is the simple code used:这是使用的简单代码:

import javax.swing.*;

class WhereToPutTheGui {

    public static void initGui() {
        for (int ii=1; ii<4; ii++) {
            JFrame f = new JFrame("Frame " + ii);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            String s =
                "os.name: " + System.getProperty("os.name") +
                "\nos.version: " + System.getProperty("os.version");
            f.add(new JTextArea(s,3,28));  // suggest a size
            f.pack();
            // Let the OS handle the positioning!
            f.setLocationByPlatform(true);
            f.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {}
                initGui();
            }
        });
    }
}

I totally agree that setLocationByPlatform(true) is the nicest way to specify a new JFrame's position but on a dual-monitor setup you can get into issues.我完全同意setLocationByPlatform(true)是指定新 JFrame 位置的最佳方式,但在双显示器设置中,您可能会遇到问题。 In my case, the child JFrame is spawned on 'the other' monitor.就我而言,子 JFrame 是在“另一个”监视器上生成的。 Example: I have my main GUI on Screen 2, I start a new JFrame with setLocationByPlatform(true) and it opens on Screen 1. So here is a more complete solution, I think:示例:我在屏幕 2 上有我的主 GUI,我使用setLocationByPlatform(true)启动了一个新的 JFrame 并在屏幕 1 上打开。所以这是一个更完整的解决方案,我认为:

...
// Let the OS try to handle the positioning!
f.setLocationByPlatform(true);
if (!f.getBounds().intersects(MyApp.getMainFrame().getBounds())) {
    // non-cascading, but centered on the Main GUI
    f.setLocationRelativeTo(MyApp.getMainFrame()); 
}
f.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM