本文介绍了设置 JFrame 位置的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 第一个和主框架应该放在哪里?总是在中心 (setLocationRelativeTo(null))?
  2. JFrame 应该放在哪里?相对于它的父 JFrame,在屏幕中央,我们想要的任何地方?
  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?

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

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?

推荐答案

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

Here is an example that incorporates the advice of:

  1. 充满鳗鱼的气垫船 - 按平台设置位置.

Aardvocate Akintayo Olu - 序列化位置.

Aardvocate Akintayo Olu - serialize the location.

但继续添加 2 个调整:

But goes on to add 2 tweaks:

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

这 4 点组合提供了最佳用户体验!

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

这篇关于设置 JFrame 位置的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:27