简体   繁体   English

应用程序自动将宽度和高度增加 10 个像素

[英]The application automatically adds 10 pixels to width and height

My question is very wierd, after searching and reading the code 40 times, i can't explain why the jpanel and jframe gets additional 10 pixels to their width and height.我的问题很奇怪,在搜索和阅读代码 40 次之后,我无法解释为什么 jpanel 和 jframe 的宽度和高度增加了 10 个像素。

the code is:代码是:

public class Game extends JPanel implements Runnable {

        public static final String NAME = "ALPHA !";

        public static final int WIDTH = 600, HEIGHT = 400;

        public static void main(String[] args) {
            Game game = new Game();
            game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
            game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
            game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
            game.setFocusable(true);
            game.requestFocusInWindow();

            JFrame frame = new JFrame(NAME);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setLayout(new BorderLayout());
            frame.add(game, BorderLayout.CENTER);
            frame.pack();

            frame.setResizable(false);
            frame.setAlwaysOnTop(true);
            frame.setLocationRelativeTo(null);

            frame.setVisible(true);
            game.startGame();
        }

        public void startGame() {
            System.out.println("w " + getWidth() + ", h " + getHeight());
            new Thread(this).start();
        }
}

The println in the startGame method prints: startGame 方法中的 println 打印:

w 610, h 410

Thanks in advance.提前致谢。

使用frame.setUndecorated(true)那么你就会明白为什么高度和宽度增加了 10pxls。

I just encountered this problem when trying to add a single custom JPanel to my JFrame, which acts as a canvas.我刚刚在尝试将单个自定义 JPanel 添加到我的 JFrame(充当画布)时遇到了这个问题。 I'm using Java 8 (update 231) and this is still an issue happened.我正在使用 Java 8(更新 231),这仍然是一个问题。

I don't exactly know why this is happening.我不完全知道为什么会这样。 In the end, the issue could be fixed by calling the frame.setResizable(false) after frame.setVisible(true) on the JFrame.最后,可以通过在 JFrame 上的frame.setVisible(true)之后调用frame.setResizable(false)来解决该问题。

Since I'm late to the party here, I hope this will help someone struggling with this as well.由于我在这里参加聚会迟到了,我希望这也能帮助那些为此苦苦挣扎的人。

Have a nice day祝你今天过得愉快

EDIT: Just saw the comments below the answer suggesting kind of the same by moving frame.setResizable(false) before frame.pack() , which works as well.编辑:刚刚看到答案下面的评论,通过在frame.setResizable(false)之前移动frame.setResizable(false)暗示类似的frame.pack() ,这也有效。 Sorry for bringing this up again.. :)很抱歉再次提出这个问题.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPanel setPreferredSize将两个额外的像素添加到宽度和高度 - JPanel setPreferredSize adds two extra pixels to width and height Java:g.drawString()宽度和高度以像素为单位? - Java: g.drawString() width and height in pixels? JDK 10中支持的计算字符串宽度(像素)的方法是什么 - What is the supported method of calculating string width (pixels) in JDK 10 如何自动设置 ImageView 宽度/高度 - how to set ImageView width/height automatically 如何在远程桌面的Swing应用程序中确定字体宽度(以像素为单位) - How to determine font width in pixels in Swing application for remote desktop Java桌面应用程序:如何确定高度和宽度 - Java Desktop Application : How to fix height and width 如何在Android中根据宽度和高度像素缩放位图? - How can I scale my bitmap in Android based on the width and height pixels? 为什么Android以不正确的方式将View绘制在另一个内(以像素为单位定义宽度和高度)? - Why does Android draw View inside another (with width and height defined in pixels) in incorrect way? 如何获取 View 文本大小的高度和宽度值(以像素为单位)? - How can I get the height and width values of View's text size in pixels? Java Swing应用程序-如何在面板中设置组件的高度(和宽度) - java swing application - how to set the height (and width) of a component in a panel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM