ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 为什么调用JFrame.pack()会增加额外的空间?

2019-07-29 07:12:59  阅读:178  来源: 互联网

标签:java user-interface swing jframe


最初,我使用的代码工作正常,但有点令人费解.将方法的某些部分移动到JFrame的构造函数后,事情正常.

除了使用pack()之外的所有东西都能使框架成为合适的尺寸.

这是原始代码:

public class BaseGameFrame extends JFrame {

public static final int WINDOWED = 0;
public static final int UFS = 1;

protected BaseGamePanel gamePanel;

public BaseGameFrame(String title, int pWidth, int pHeight, long period, int windowType){
    super(title);

    switch(windowType){
        case UFS:
                    this.setUndecorated(true);

                    Rectangle screenSize = this.getGraphicsConfiguration().getBounds();
                    pWidth = screenSize.width;
                    pHeight = screenSize.height;

                    break;

        default:    break;
    }

    this.setVisible(true);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.createPanel(pWidth, pHeight, period);


}

protected void createPanel(final int pWidth, final int pHeight, long period){
    this.gamePanel = new BaseGamePanel(pWidth, pHeight, period);

    this.add(this.gamePanel);
    this.pack();
}

public static void main(String[] args){
    new BaseGameFrame("Test", 800, 600, 20L * 1000000L, UFS);
}

}

这是在修改它之后:

public class BaseGameFrame extends JFrame {


protected BaseGamePanel gamePanel;

public BaseGameFrame(String title, VideoType vType, BaseGamePanel gp){
    super(title);

    switch(vType){
        case UFS:   
                    this.setUndecorated(true);

                    Rectangle screenSize = this.getGraphicsConfiguration().getBounds();
                    gp.setPDimensions(new Dimension(screenSize.width, screenSize.height));

                    break;

        default:    break;
    }


    this.add(gp);
    this.pack();

    this.setVisible(true);

    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public static void main(String[] args){
    BaseGamePanel gp = new BaseGamePanel(800, 600, 20L * 1000000L);
    new BaseGameFrame("Test", VideoType.UFS, gp);
}

}

我不太确定问题是什么……但最终发生的事情是:

解决方法:

确保在调用pack()或setVisible(true)之前调用setResizable(false)

标签:java,user-interface,swing,jframe
来源: https://codeday.me/bug/20190729/1569244.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有