Why does the JFrame setSize() method not set the size correctly?

后端 未结 6 2095
名媛妹妹
名媛妹妹 2020-12-16 12:40

So I\'ve been programming in java for a semester or so, and I\'ve had this problem a few times and finally got around to asking.

If I make a JFrame and

6条回答
  •  旧时难觅i
    2020-12-16 13:29

    There are lots of good reasons for setting the size of a frame. One is to remember the last size the user set, and restore those settings. I have this code which seems to work for me:

    package javatools.swing;
    import java.util.prefs.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.JFrame;
    
    public class FramePositionMemory {
        public static final String WIDTH_PREF = "-width";
    
        public static final String HEIGHT_PREF = "-height";
    
        public static final String XPOS_PREF = "-xpos";
    
        public static final String YPOS_PREF = "-ypos";
        String prefix;
        Window frame;
        Class cls;
    
        public FramePositionMemory(String prefix, Window frame, Class cls) {
            this.prefix = prefix;
            this.frame = frame;
            this.cls = cls;
        }
    
        public void loadPosition() {
            Preferences prefs = (Preferences)Preferences.userNodeForPackage(cls);
        //  Restore the most recent mainframe size and location
            int width = prefs.getInt(prefix + WIDTH_PREF, frame.getWidth());
            int height = prefs.getInt(prefix + HEIGHT_PREF, frame.getHeight());
            System.out.println("WID: " + width + " HEI: " + height);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int xpos = (screenSize.width - width) / 2;
            int ypos = (screenSize.height - height) / 2;
            xpos = prefs.getInt(prefix + XPOS_PREF, xpos);
            ypos = prefs.getInt(prefix + YPOS_PREF, ypos);
            frame.setPreferredSize(new Dimension(width, height));
            frame.setLocation(xpos, ypos);
            frame.pack();
        }
    
        public void storePosition() {
            Preferences prefs = (Preferences)Preferences.userNodeForPackage(cls);
            prefs.putInt(prefix + WIDTH_PREF, frame.getWidth());
            prefs.putInt(prefix + HEIGHT_PREF, frame.getHeight());
            Point loc = frame.getLocation();
            prefs.putInt(prefix + XPOS_PREF, (int)loc.getX());
            prefs.putInt(prefix + YPOS_PREF, (int)loc.getY());
            System.out.println("STORE: " + frame.getWidth() + " " + frame.getHeight() + " " + loc.getX() + " " + loc.getY());
        }
    }
    public class Main {
    void main(String[] args) {
            JFrame frame = new Frame();
            // SET UP YOUR FRAME HERE.
            final FramePositionMemory fm = new FramePositionMemory("scannacs2", frame, Main.class);
            frame.setSize(400, 400); // default size in the absence of previous setting
            fm.loadPosition();
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    fm.storePosition();
                }
            });
            frame.setVisible(true);
        }
    }
    

    }

提交回复
热议问题