如何从构造函数外部使用静态方法
调整
JFrame
?
让我解释一下为什么我需要这个。
我正在用 Java 编写类似塞尔达传说的游戏,并且我有一个地牢。在我的地牢里,有普通大小的房间,还有boss房间。所以我的问题是,是否可以在游戏后期调整我的 JFrame
大小,因为 Boss 房间的尺寸与其他房间的尺寸不同?我直接在 JFrame
上绘图。
我尝试使用setSize(int,int)
,但它只能用作non-static
。
如果我问的不清楚,请告诉我。
感谢阅读
最佳答案
How do I resize a JFrame from outside the constructor and with a static method ?
- 创建 JFrame 作为局部变量,不要扩展 JFrame
So my question is, is it possible to resize my JFrame later on the game since the Boss room's dimensions are different from the others' ?
- 是的,我更喜欢 JFrame.pack(然后它的 child 返回适当的 PreferredSize 并由适当的 LayoutManager 放置),而不是 setSize(int, int) 或 setPreferredSize(new Dimension(int, int)) 和 JFrame.pack (),所有提到的三个选择在所有情况下都适用于运行时
I'm drawing directly on the JFrame.
- 这是错误的决定,使用 JPanel 进行绘画,为 JFrame.pack() 覆盖 JPanels getPreferredSize,然后根据 getPreferredSize 计算 JFrame 和自定义 paiting 的所有坐标
I tried using setSize(int,int), but it can only be used as non-static.
- 没有 SSCCE 就不知道了
关于JAVA 如何在运行时调整 JFrame 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17700051/