java jframe pack_java – 为什么调用JFrame.pack()会增加额外的...

最初,我使用的代码工作正常,但有点令人费解.将方法的某些部分移动到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);

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值