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

标签 java swing user-interface 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);
}

}

我不太确定问题出在哪里.. 但最终发生的是这样的:this is an issue

最佳答案

确保在调用 pack()setVisible(true) 之前调用 setResizable(false)/p>

关于java - 为什么调用 JFrame.pack() 会增加额外的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595897/

相关文章:

java - 将成员添加到对象数组

java - Swing 组件的开源改进或替换

java仿射变换正确顺序

MFC 中的 C++ GUI - 分页小部件

iphone - 如何禁用文本字段?

ios - 如何让 titleView 在推送/弹出转换期间保留在 UINavigationBar 中,如 Facebook 应用程序?

java - 线程因 sleep 而暂停后会发生什么?

java - 如果从 jar 运行,带有嵌入式 jetty 的 Spring 应用程序找不到 webdefault.xml

java - 如何使用 Post 请求将 JSON 字符串传递给 Jersey Rest Web 服务

java - JScrollPane 内的 MigLayout-Component 强制收缩