有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

设置JFrame位置的java最佳实践

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

  1. 第一个机架和主机架应位于何处?总是在中心(setLocationRelativeTo(null)
  2. 孩子应该在哪里?相对于其父JFrame,在屏幕的中心,在我们想要的任何地方

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


共 (4) 个答案

  1. # 1 楼答案

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

    1. 满是鳗鱼的气垫船

    2. Aardvocate Akintayo Olu——将位置序列化

    但接下来添加了两个调整:

    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);
        }
    }
    
  2. # 2 楼答案

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

  3. # 3 楼答案

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

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

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

  4. # 4 楼答案

    我通常会通过打电话让平台决定:

    myJFrame.setLocationByPlatform(true);
    

    这使窗口“出现在本机窗口系统的默认位置”。更多信息:Window API