设置JFrame位置的最佳做法

时间:2011-10-15 11:47:43

标签: java swing user-interface jframe

我有一个(有点哲学)问题相对于Swing,或者一般的GUI编程。是否有关于在哪里找到应用程序中使用的JFrame实例的公认最佳实践?

  1. 第一帧和主框架应该放在哪里?始终位于中心(setLocationRelativeTo(null))?
  2. 应该将孩子JFrame放在哪里?相对于其父JFrame,在屏幕的中心,我们想要的任何地方?
  3. 我一直认为有一些最好的做法,有点像这样的“GUI圣经”,我错了,我应该(喘气)任意决定该做什么?

4 个答案:

答案 0 :(得分:25)

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

  1. 充满鳗鱼的气垫船 - set location by platform

  2. Aardvocate Akintayo Olu - 序列化位置。
  3. 但继续增加2个调整:

    1. 同时序列化宽度/高度。
    2. 如果帧在关闭时最大化,则在获取边界之前恢复。 (我厌恶应用程序。序列化选项,但不考虑这一点。用户坐在那里点击'最大化/恢复'按钮&想知道为什么没有发生任何事情!
    3. 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);
          }
      }
      

答案 1 :(得分:9)

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

myJFrame.setLocationByPlatform(true);

这使窗口“显示在本机窗口系统的默认位置”。有关详情:Window API

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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

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

关于子框架,取决于其大小,在父框架的中心,或只是易于使用的东西。