【发布时间】: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);
}
}
我不太确定问题出在哪里.. 但最终发生的是:
【问题讨论】:
-
听起来问题更可能出在
BaseGamePanel。pack只是使用首选大小的内容窗格来确定框架的大小(或多或少) -
我已经单步执行该程序有一段时间了,面板的
preferredSize显示为600, 600,但实际的width和height变为610一旦pack被调用.. -
不要忘记框架可能会增加额外的空间来处理框架。内容窗格的首选大小是多少(打包后)?另外,你有没有给任何东西添加边框??
-
首选尺寸从未改变;在
pack()之前调用setResizable(false)是问题所在。
标签: java swing user-interface jframe