【问题标题】:Why is calling JFrame.pack() adding extra space?为什么调用 JFrame.pack() 会增加额外的空间?
【发布时间】:2013-01-13 18:58:03
【问题描述】:

最初,我使用的代码运行良好,但有点复杂。将方法的某些部分移入 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);
}

}

我不太确定问题出在哪里.. 但最终发生的是:

【问题讨论】:

  • 听起来问题更可能出在BaseGamePanelpack 只是使用首选大小的内容窗格来确定框架的大小(或多或少)
  • 我已经单步执行该程序有一段时间了,面板的preferredSize 显示为600, 600,但实际的widthheight 变为610一旦pack 被调用..
  • 不要忘记框架可能会增加额外的空间来处理框架。内容窗格的首选大小是多少(打包后)?另外,你有没有给任何东西添加边框??
  • 首选尺寸从未改变;在pack() 之前调用setResizable(false) 是问题所在。

标签: java swing user-interface jframe


【解决方案1】:

确保在调用pack()setVisible(true)

之前调用setResizable(false)

【讨论】:

  • 那行得通...当setResizable(false) 被调用时插入是否被修改?
  • @mmnumbp:老实说,我不知道为什么这样做很重要。我只知道它是这样工作的。
  • @HovercraftFullOfEels 哇,没想到这会是个问题。每天学习新东西+1
  • pack() 之前调用setResizable(false) 的原因是窗口“chrome”(窗口边缘的装饰)可能会在可调整大小和不可调整大小模式之间变化。通过在设置 resizable 属性后打包窗口,API 将考虑用于不可调整大小的窗口的(通常更薄的)镶边。
  • 希望你不介意我“加入”我两年前看到并投票的答案,但我只是在 different thread 上链接了这个答案,以及 OP需要稍微澄清一下建议。随意将其编辑为答案(如果您认为合适)。 :)
猜你喜欢
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 2020-12-13
  • 2020-10-08
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多