在屏幕上设置JFrame位置并存储属性

时间:2013-05-27 10:45:11

标签: java swing jframe

我正在写一个java程序,我从这里使用了Andrew Thompson的代码:Best practice for setting JFrame locations

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. 我设置了全屏窗口(JFrame)然后关闭它,当我 重新打开窗口,它具有全屏的尺寸,但它是 不是全屏。
  2. 这个2°问题与汤普森的代码无关, 当我将窗口移动到屏幕边框时,它不会落后于屏幕 屏幕边框并与屏幕边缘并排。
  3. 目前关于“option.prop”文件 该文件存储在我的.jar父文件夹中,我想存储 它在我的.jar程序的同一文件夹中,我该怎么办?
  4. 由于

1 个答案:

答案 0 :(得分:2)

这是一个基于安德鲁的略微修改的例子。

解决1.和3.(考虑问题评论)。它在最大化之前跟踪框架的位置和大小。显然,调用f.setExtendedState(Frame.NORMAL)不会立即将帧重新调整为原始大小。

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

class RestoreMe {

    /**
     * This will end up in a sub-directory of user.home. 
     * (exercise done by the reader)
     */
    public static final String fileDir = 
            System.getProperty("user.home") 
            + System.getProperty("file.separator") 
            + ".restoreMe";
    public static final String fileName = 
            fileDir
            + System.getProperty("file.separator")
            + "props.file";


    /**
     * Store location & size of UI
     */
    public static void storeOptions(Frame f, Properties p) throws Exception {
        File file = new File(fileName);

        // only need to update extended state in properties
        p.setProperty("extState", String.valueOf(f.getExtendedState()));

        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, Properties p) throws IOException {
        File file = new File(fileName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        p.load(br);

        int extState = Integer.parseInt(p.getProperty("extState"));

        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);
        f.setExtendedState(extState);
    }

    public static void main(String[] args) {
        // we keep track of a single instance of properties
        final Properties p = new Properties();
        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, p);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }            
        });
        // keep track of frame movement and update properties accordingly
        f.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) {
                    Dimension d = f.getSize();
                    int w = (int) d.getWidth();
                    int h = (int) d.getHeight();                    
                    p.setProperty("w", "" + w);
                    p.setProperty("h", "" + h);
                }
            }
            @Override
            public void componentMoved(ComponentEvent e) {
                if (f.getExtendedState() != JFrame.MAXIMIZED_BOTH) {
                    Point l = f.getLocation();
                    int x = (int) l.getX();
                    int y = (int) l.getY();
                    p.setProperty("x", "" + x);
                    p.setProperty("y", "" + y);
                }
            }            
        });
        JTextArea ta = new JTextArea(20, 50);
        f.add(ta);
        f.pack();

        // create directory hierarchy for our app
        (new File(fileDir)).mkdirs();

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

对于2.,您不能指望它得到回答,因为您没有发布与之相关的代码。我建议你为此创建另一个问题。