如何将 Java window(实际上是 JFrame)设置为正确的分辨率?
How do I set my Java window (JFrame, actually) to the correct resolution?
我在 Java 中通过修改 Jamal's pong game and now I've noticed that the entire window 字面意思是 800x600,但这不是我想要的! 我希望 frame 是 800x600 并且 是 window 8 的花式边界。向 Java 询问 800x600 JFrame,然后得到 794x571 框架,这对我来说意义不大!
设置window的主要代码:
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
table = new Table(this);
add(table);
}
我发现的原因是因为我改变了racket/paddle速度,racket/paddles卡在了window的两端,所以修改代码后桨没有不再坚持了,但现在他们移出了可见框架一点点(因为我在逻辑中使用 0 和 window 的高度来停止 racket/paddle)
现在我想知道有多少 Java windows 实际上大小不对...
尝试 getContentPane().setPreferredSize(cvars.get("Window", "width", int.class, 800),cvars.get("Window", "height", int.class, 600));
并在显示 window 后调用 pack()
。
或者您也可以设置 table 的首选大小。
创建一个包含 Table
的 JPanel
,在其中覆盖它的 getPreferredSize
方法和 return 您想要的尺寸
public class TableWrapperPanel extends JPanel {
private Table table;
public TableWrapperPanel(Table table) {
setLayout(new BorderLayout());
this.table = table;
add(table);
}
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
现在,在您的主要方法中,创建 TableWrapperPanel
的一个实例并将其添加到您的 "window",完成后调用 pack
,这将打包 window 围绕您的内容。
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableWrapperPanel wrapper = new TableWrapper(table);
add(wrapper );
pack();
setVisible(true);
}
或者,您可以创建 Table
的自定义版本,它会覆盖 getPreferredSize
方法本身
public class SizableTable extends Table {
//... Constructors ...
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
并且不是在 main
方法中创建 Table
的实例,而是创建 SizableTable
的实例,将其添加到 window 然后调用 pack
我在 Java 中通过修改 Jamal's pong game and now I've noticed that the entire window 字面意思是 800x600,但这不是我想要的! 我希望 frame 是 800x600 并且 是 window 8 的花式边界。向 Java 询问 800x600 JFrame,然后得到 794x571 框架,这对我来说意义不大!
设置window的主要代码:
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
table = new Table(this);
add(table);
}
我发现的原因是因为我改变了racket/paddle速度,racket/paddles卡在了window的两端,所以修改代码后桨没有不再坚持了,但现在他们移出了可见框架一点点(因为我在逻辑中使用 0 和 window 的高度来停止 racket/paddle)
现在我想知道有多少 Java windows 实际上大小不对...
尝试 getContentPane().setPreferredSize(cvars.get("Window", "width", int.class, 800),cvars.get("Window", "height", int.class, 600));
并在显示 window 后调用 pack()
。
或者您也可以设置 table 的首选大小。
创建一个包含 Table
的 JPanel
,在其中覆盖它的 getPreferredSize
方法和 return 您想要的尺寸
public class TableWrapperPanel extends JPanel {
private Table table;
public TableWrapperPanel(Table table) {
setLayout(new BorderLayout());
this.table = table;
add(table);
}
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
现在,在您的主要方法中,创建 TableWrapperPanel
的一个实例并将其添加到您的 "window",完成后调用 pack
,这将打包 window 围绕您的内容。
public Main() {
// cvars contains variables from JPong.ini (It's an extended class from the ini4j library)
cvars = CVarList.getInstance();
// get(sectionName, optionName, classtype, defaultValue)
setSize(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
setTitle(cvars.get("Window", "title", String.class, "JPong"));
setResizable(cvars.get("Window", "resizable", boolean.class, false));
setDefaultCloseOperation(EXIT_ON_CLOSE);
TableWrapperPanel wrapper = new TableWrapper(table);
add(wrapper );
pack();
setVisible(true);
}
或者,您可以创建 Table
的自定义版本,它会覆盖 getPreferredSize
方法本身
public class SizableTable extends Table {
//... Constructors ...
public Dimension getPreferredSize() {
cvars = CVarList.getInstance();
return new Dimension(cvars.get("Window", "width", int.class, 800),
cvars.get("Window", "height", int.class, 600));
}
}
并且不是在 main
方法中创建 Table
的实例,而是创建 SizableTable
的实例,将其添加到 window 然后调用 pack