JFrame isResizable(false) 大小问题

JFrame isResizable(false) sizing issue(JFrame isResizable(false) 大小问题)

本文介绍了JFrame isResizable(false) 大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算制作一个 ContentPanel 为 600x600 的 JFrame,并且我希望 JFrame 不能重新调整大小.在这个盒子里,我画了一个 600x600 的红色轮廓矩形,以确保在我运行程序时一切都匹配.在限制 JFrame 的大小调整之前,我通过以下方式设置 JFrame 的大小:

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();

当我启动程序时,我的矩形边界与 JFrame 的尺寸完美契合.但是,当我将 isResizable(false) 添加到等式中时,矩形的右边缘和矩形的底边缘之间似乎存在像素缓冲区.通过一些试验和错误,似乎 isResizable(false) 在高度和宽度上增加了 10 个像素.(因此 contentPane 的尺寸为 590x590 + 额外的 10 是我的解决方法)

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)

我的问题 为什么会这样?我似乎在任何地方都找不到关于额外 10 像素的任何文档?

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

注意:在不修改大小的情况下制作 JFrame 也可以观察到这种异常情况.如果不使其不可调整大小,则没有可观察的面板/内容窗格,但是当使用 isResizable(false) 时,面板/窗格的可见部分.

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.

推荐答案

你是对的,将一个框架设置为 un-resiable 似乎确实增加了 10 个像素的高度和宽度,至于为什么,我不能说,这似乎是更新本机对等体的副作用,但是......

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...

你可以在调用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);
        }

    }

}

这篇关于JFrame isResizable(false) 大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JFrame isResizable(false) 大小问题