【问题标题】:How to best position Swing GUIs?如何最好地定位 Swing GUI?
【发布时间】:2011-10-31 21:06:12
【问题描述】:

another thread 中,我表示我喜欢通过执行以下操作来使我的 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);

但 Andrew Thompson 有不同的意见,改为致电

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

好奇的人想知道为什么?

【问题讨论】:

  • gui 应该从上次结束的地方开始。

标签: java swing user-interface


【解决方案1】:

我完全同意setLocationByPlatform(true) 是指定新 JFrame 位置的最佳方式,但在 双显示器设置 上您可能会遇到问题。就我而言,子 JFrame 是在“另一个”监视器上生成的。示例:我在屏幕 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);

【讨论】:

    【解决方案2】:

    在我看来,屏幕中间的 GUI 看起来如此..“闪屏”。我一直在等待它们消失,真正的 GUI 出现!

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

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

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

    (3 批)3 个 GUI 整齐堆叠。这代表了最终用户的“最不意外的路径”,因为它是操作系统如何定位默认纯文本编辑器(或其他任何东西)的 3 个实例的方式。感谢 Linux 和 Mac 的垃圾神。图片。

    这里是使用的简单代码:

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

    【讨论】:

    • @AndrewThompson 为什么你的计数器变量是ii 而不仅仅是i?这是否符合某种惯例,还是个人喜好(或者可能完全不同)?
    • @MirroredFate 嗯.. 我想我会在那里锁定选项 3,“完全不同的东西”。这正是我第一次使用 Basic 编程时所习惯的(是的,很久以前)。懒惰是继续使用的原因,“如果它没有坏,就不要修理它”。
    • @MirroredFate 你是波斯王子范吗?我很抱歉把它放在这里。我就是忍不住
    • @MirroredFate 这就是我使用ii 而不是i 的原因。当我参加编程比赛时,我经常不得不搜索循环索引,例如,+1-1 以修复一个错误。在这些情况下,搜索ii 比搜索i 容易得多,无论我使用哪个编辑器。同样,我使用jjkk 作为嵌套循环索引。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多