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

查看:146
本文介绍了如何最好地定位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);

但Andrew Thompson有不同意见,而是致电

But Andrew Thompson had a different opinion, to instead call

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

并且询问的头脑想知道原因?

and inquiring minds want to know why?

推荐答案

在我看来,屏幕中间的GUI看起来如此......splash-screen'ish。我一直在等待它们消失,真正的 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..


设置此窗口是否应出现在本机窗口系统的默认位置或当前位置(由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个整齐堆叠的图形用户界面。这代表了最终用户的最小惊喜之路,因为它是操作系统如何定位默认纯文本编辑器的3个实例(或其他任何东西)。我要感谢Linux和Linux的trashgod。苹果电脑。图片。

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

以下是使用的简单代码:

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

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

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