设置 JFrame 位置的最佳实践

发布于 2024-12-10 05:14:52 字数 316 浏览 0 评论 0原文

我有一个关于 Swing 或一般 GUI 编程的(有点哲学的)问题。是否有公认的最佳实践来确定应用程序中使用的 JFrame 实例的位置?

  1. 第一个框架和主框架应该位于哪里?始终位于中心 (setLocationRelativeTo(null))?
  2. JFrame 应该位于哪里?相对于它的父级 JFrame,位于屏幕的中心,我们想要的任何位置?

我一直认为有一些最佳实践,有点像“GUI 圣经”,我错了吗?我应该(喘气)任意决定做什么?

I have a (somewhat philosophical) question relatively to Swing, or to GUI programming in general. Are there recognized best practices on where to locate the JFrame instances used in the application?

  1. Where should the first and main frame be located? Always at the center (setLocationRelativeTo(null))?
  2. Where should a child JFrame be located? Relatively to its parent JFrame, at the center of the screen, wherever we want?

I have always assumed there were some best practices, kind of a "GUI bible" about this, am I wrong and should I (gasp) arbitrarily decide what to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀念你的温柔 2024-12-17 05:14:52

以下是一个包​​含以下建议的示例:

  1. Hovercraft Full Of Eels - 按平台设置位置

  2. Aardvocate Akintayo Olu - 序列化位置.

但继续添加 2 个调整:

  1. 也序列化宽度/高度。
  2. 如果框架在关闭时最大化,则在获得边界之前将其恢复。 (我讨厌那些序列化选项但没有考虑到这一点的应用程序。用户坐在那里单击“最大化/恢复”按钮并想知道为什么没有发生任何事情!

4 点组合优惠最佳用户体验!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;

class RestoreMe {

    /** This will end up in the current directory
    A more sensible location is a sub-directory of user.home.
    (left as an exercise for the reader) */
    public static final String fileName = "options.prop";

    /** Store location & size of UI */
    public static void storeOptions(Frame f) throws Exception {
        File file = new File(fileName);
        Properties p = new Properties();
        // restore the frame from 'full screen' first!
        f.setExtendedState(Frame.NORMAL);
        Rectangle r = f.getBounds();
        int x = (int)r.getX();
        int y = (int)r.getY();
        int w = (int)r.getWidth();
        int h = (int)r.getHeight();

        p.setProperty("x", "" + x);
        p.setProperty("y", "" + y);
        p.setProperty("w", "" + w);
        p.setProperty("h", "" + h);

        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        p.store(br, "Properties of the user frame");
    }

    /** Restore location & size of UI */
    public static void restoreOptions(Frame f) throws IOException {
        File file = new File(fileName);
        Properties p = new Properties();
        BufferedReader br = new BufferedReader(new FileReader(file));
        p.load(br);

        int x = Integer.parseInt(p.getProperty("x"));
        int y = Integer.parseInt(p.getProperty("y"));
        int w = Integer.parseInt(p.getProperty("w"));
        int h = Integer.parseInt(p.getProperty("h"));

        Rectangle r = new Rectangle(x,y,w,h);

        f.setBounds(r);
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Good Location & Size");
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    storeOptions(f);
                } catch(Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
        });
        JTextArea ta = new JTextArea(20,50);
        f.add(ta);
        f.pack();

        File optionsFile = new File(fileName);
        if (optionsFile.exists()) {
            try {
                restoreOptions(f);
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            f.setLocationByPlatform(true);
        }
        f.setVisible(true);
    }
}

Here is an example that incorporates the advice of:

  1. Hovercraft Full Of Eels - set location by platform.

  2. Aardvocate Akintayo Olu - serialize the location.

But goes on to add 2 tweaks:

  1. Serialize the width/height as well.
  2. If the frame is maximized at time of close, it is restored before getting the bounds. (I detest apps. that serialize options but do not take that into account. The user is sitting there clicking the 'Maximize / Restore' button & wondering why nothing is happening!)

The 4 points combined offer the best user experience!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;
import java.io.*;

class RestoreMe {

    /** This will end up in the current directory
    A more sensible location is a sub-directory of user.home.
    (left as an exercise for the reader) */
    public static final String fileName = "options.prop";

    /** Store location & size of UI */
    public static void storeOptions(Frame f) throws Exception {
        File file = new File(fileName);
        Properties p = new Properties();
        // restore the frame from 'full screen' first!
        f.setExtendedState(Frame.NORMAL);
        Rectangle r = f.getBounds();
        int x = (int)r.getX();
        int y = (int)r.getY();
        int w = (int)r.getWidth();
        int h = (int)r.getHeight();

        p.setProperty("x", "" + x);
        p.setProperty("y", "" + y);
        p.setProperty("w", "" + w);
        p.setProperty("h", "" + h);

        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        p.store(br, "Properties of the user frame");
    }

    /** Restore location & size of UI */
    public static void restoreOptions(Frame f) throws IOException {
        File file = new File(fileName);
        Properties p = new Properties();
        BufferedReader br = new BufferedReader(new FileReader(file));
        p.load(br);

        int x = Integer.parseInt(p.getProperty("x"));
        int y = Integer.parseInt(p.getProperty("y"));
        int w = Integer.parseInt(p.getProperty("w"));
        int h = Integer.parseInt(p.getProperty("h"));

        Rectangle r = new Rectangle(x,y,w,h);

        f.setBounds(r);
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Good Location & Size");
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        f.addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                try {
                    storeOptions(f);
                } catch(Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
        });
        JTextArea ta = new JTextArea(20,50);
        f.add(ta);
        f.pack();

        File optionsFile = new File(fileName);
        if (optionsFile.exists()) {
            try {
                restoreOptions(f);
            } catch(IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            f.setLocationByPlatform(true);
        }
        f.setVisible(true);
    }
}
扛刀软妹 2024-12-17 05:14:52

我通常让平台通过调用来决定:

myJFrame.setLocationByPlatform(true);

这让窗口“出现在本机窗口系统的默认位置”。有关详细信息:Window API

I've usually let the platform decide by calling:

myJFrame.setLocationByPlatform(true);

This lets the window "appear at the default location for the native windowing system". For more on this: Window API

爱,才寂寞 2024-12-17 05:14:52

我总是做的是从主框架的屏幕中心开始,或者从子框架的父级中心开始,我记录这个位置。然后,当用户将框架移动到他们想要的任何位置时,我记录新位置,当下次启动应用程序时,我使用最后一个位置来放置框架。

What I always do is start at the center of the screen for main frame, or at the center of a parent for child frames, I record this location. Then as users move the frames to wherever they want I record the new location and when next the app is started, I use the last location to place the frame.

放飞的风筝 2024-12-17 05:14:52

不确定是否有最佳实践,因为它非常主观。

将其设置在中心并允许用户将其更改为他们喜欢的位置似乎是理想的选择。

至于子框架,取决于它的大小,位于父框架的中心,或者只是一些易于使用的东西。

Not sure if there's a best practice as it is very subjective.

Setting it at the center and allowing users to change it to the location they like seems to be the ideal one.

As regards to the child frame, depending on its size, in the center of the parent frame, or just something easy to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文