我想获取我的 JFrame 的高度和宽度,以便即使调整窗口大小,图形也应处于相同的相对位置。为此,我试图在我的构造函数中获取高度和宽度,但它始终返回 0。执行此操作的最佳方法是什么?
public class FireworkComponent extends JComponent {
public FireworkComponent() {
//some variables ....
this.getHeight();
this.getWidth();
}
最佳答案
如果组件未实现(即可见),尺寸将返回 0
。如果您覆盖组件的 paintComponent
方法,您将能够检索容器的尺寸并通过在组件的 Graphics
对象上绘制来在特定位置设置图形。
关于java - 在构造函数中获取 JComponent 的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8070113/