Why is calling JFrame.pack() adding extra space?(为什么调用 JFrame.pack() 会增加额外的空间?)
问题描述
最初,我使用的代码运行良好,但有点复杂.将方法的某些部分移入 JFrame 的构造函数后,一切正常.
Originally, the code I was using worked fine, but was a bit convoluted. After moving some parts of a method into the constructor for the JFrame, things were working properly.
除了使用 pack() 使框架大小合适之外的所有内容.
Everything except using pack() to make the frame the proper size.
这里是原始代码:
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);
}
}
我不太确定问题出在哪里.. 但最终发生的是:
I'm not quite sure what the problem is.. but what ends up happening is this:
推荐答案
确保调用 setResizable(false) 在调用 pack() 或 setVisible(true)
Make sure to call setResizable(false) before calling pack() or setVisible(true)
这篇关于为什么调用 JFrame.pack() 会增加额外的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么调用 JFrame.pack() 会增加额外的空间?
				
        
 
            
        - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 
						
						
						
						
						
				
				
				
				