java中的菜鸟作业问题窗口尺寸

发布于 2024-08-21 01:28:55 字数 241 浏览 6 评论 0原文

我是一名高中生,在斯坦福大学通过视频学习 cs106a。

对于我当前的作业,我必须添加 GObject 并将它们相对于窗口的大小定位。

我目前正在尝试使用命令获取窗口的宽度,

int width = getWidth();

但是 width = 0

可能导致此问题的一件事: 这是我使用多个类编写的第一个程序。

感谢您的帮助!

I am a high school student taking cs106a at Stanford via video.

For my current assignment I have to add GObjects and position them relative to the size of the window.

I am currently trying to get the width of the window using the command

int width = getWidth();

however width = 0

One thing that could be causing this:
this is one of the first programs I have written using multiple classes.

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

止于盛夏 2024-08-28 01:28:55

一个可能的原因 - 您可能在窗口(我假设 JFrame)实现之前调用 getWidth() (又名 setVisible(true)) code> 或 pack() 调用)。 getWidth()在实现之前会返回0。

One possible cause - you may be calling getWidth() before the window (I'm assuming JFrame) has been realized (aka had setVisible(true) or pack() called on it). getWidth() will return 0 before it is realized.

朱染 2024-08-28 01:28:55

亚历克斯,

在没有您提供的示例代码的情况下,我将进行大胆的猜测,并说您正在类的构造函数中检查宽度,或者在创建底层操作系统窗口之前的其他时间,或者在它被摧毁之后。

如果您尝试在这些时间内获取窗口属性,您可能会得到零或其他一些无意义的结果。

Alex,

In the absence of example code from you, I'm going to take a wild guess and say that you're checking width in the constructor of your class, or at some other time either before the underlying OS window has been created, or after it has been destroyed.

If you try to get window attributes during these times, you'll probably get zero or some other nonsense result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文