如何最好地定位 Swing GUI? [英] How to best position Swing GUIs?

查看:19
本文介绍了如何最好地定位 Swing GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个线程中 我说过我喜欢通过做这样的事情来使我的 GUI 居中:

In another thread I stated that I liked to center my GUIs by doing something like this:

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);

好奇心想知道为什么?

推荐答案

在我看来,屏幕中间的 GUI 看起来很……闪屏".我一直在等待它们消失,真正的 GUI 出现!

To my eye, a GUI in the middle of the screen looks so.. "splash-screen'ish". I keep waiting for them to disappear and the real GUI to appear!

从 Java 1.5 开始,我们可以访问 Window.setLocationByPlatform(boolean).哪个..

Since Java 1.5 we've had access to Window.setLocationByPlatform(boolean). which..

设置此 Window 应出现在本机窗口系统的默认位置,还是在下次使该窗口可见时出现在当前位置(由 getLocation 返回).此行为类似于未以编程方式设置其位置的本机窗口.大多数窗口系统级联窗口,如果它们的位置没有明确设置.一旦窗口显示在屏幕上,实际位置就确定了.

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. 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.

看看这个例子的效果,它将 3 个 GUI 置于操作系统选择的默认位置 - 在 Windows 7、Linux 和 Gnome 上Mac OS X.

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 批)3 个 GUI 整齐堆叠.这代表了最终用户的最不意外的路径",因为它是操作系统如何定位默认纯文本编辑器(或其他任何东西)的 3 个实例的方式.我感谢垃圾神为 Linux &苹果电脑.图片.

(3 lots of) 3 GUIs neatly stacked. 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). My thanks to trashgod for the Linux & Mac. images.

这是使用的简单代码:

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();
            }
        });
    }
}

这篇关于如何最好地定位 Swing GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆