Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

java - JFrame isResizable(false) sizing issue

I intended to make a JFrame with a ContentPanel of 600x600 and I wanted the JFrame to be not re-sizable. Inside this box, I Drew a 600x600 red-outlined rectangle to make sure that everything matched when i ran the program. Before restricting resizing for the JFrame, I set the size of my JFrame by doing:

getContentPane().setPreferredSize( new Dimension(600,600));
pack();

And when I launched the program and the boundaries of my rectangle fit perfectly with the dimensions of the JFrame. However, when i added isResizable(false) into the equation, there seemed to be buffer of pixels between the right edge of my rectangle as well as the bottom edge of my rectangle. With a little trial and error, it seems as though isResizable(false) adds 10 pixels to height and width. (So having dimension of 590x590 for contentPane + the extra 10 is my workaround)

My Questions why is this? I can't seem to find any documentation about the extra 10 pixels anywhere?

Note: This anomaly was also also observed by making a JFrame without modifying the size. Without making it un-resizable, there is no observable panel/contentPane, but when isResizable(false) is used, there is visible part of the panel/pane.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are right, setting a frame to un-resiable does seem to add 10 pixels to it's height and width, as to why, I can't say, this seems to be side effect of updating the native peer, however...

You can reset it by call JFrame#pack after the calling JFrame#setResizable

public class TestResizableFrame {

    public static void main(String[] args) {
        new TestResizableFrame();
    }

    public TestResizableFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FixedPane());
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FixedPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Dimension size = getSize();
            String text = size.width + "x" + size.height;
            FontMetrics fm = g.getFontMetrics();
            int x = (getWidth()- fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g.drawString(text, x, y);
            g.setColor(Color.RED);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...