Java JPanel/GUI 内容仅在运行时有时出现

发布于 2024-11-27 06:29:40 字数 496 浏览 1 评论 0原文

我是 Java 编程新手,但我已经使用 C++ 有一段时间了。整个 GUI 的东西对我来说都是新的。我创建了两个 JPanel 并使用 FlowLayout 将它们添加到 JFrame 中。当我运行该程序时,我得到 http://imageshack.us/photo/ my-images/43/36213853.jpg/ 而不是 http://imageshack.us/photo/my-images/88/86682510.jpg/ 仅在某些时候。其他时候,内容显示得很好 由于某种原因,当我在窗口为空白时调整窗口大小时,内容显示正常。

我不确定是什么问题导致了我这种不一致!

任何帮助将不胜感激。谢谢!

i'm new to Java programming, but I've done C++ for a while now. the whole GUI thing is new to me. i created two JPanels and added them to JFrame using FlowLayout. When I run the program, i get http://imageshack.us/photo/my-images/43/36213853.jpg/ as opposed to http://imageshack.us/photo/my-images/88/86682510.jpg/ only SOME of the times. the other times, the content appears just fine
for some reason, when i resize the window when the window is blank, the contents appear fine.

i'm not sure what the problem is that gives me this inconsistency!

any help would be appreciated. thanks!

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

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

发布评论

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

评论(1

晨与橙与城 2024-12-04 06:29:40

没有代码很难回答,但是您是否有机会在打包 GUI 之前以及向 GUI 添加组件之前调用 setVisible(true) ?如果调整 GUI 的大小,组件会显示吗?

如果是这样,您只需要在 GUI 完全填充组件之后(通常是在打包之后)才进行此调用(以便您的布局管理器可以布置所有组件并调整组件的大小)图形用户界面正确)。

如果此建议没有帮助,那么您可能需要发布一些有问题的代码。发布多少内容(很难说)足以让我们识别问题;)但不要太多,以免淹没在与您的问题无关的代码中。如果您有能力创建一个,那么最好发布的代码是 SSCCE

It's hard to answer without code, but by chance are you calling setVisible(true) before packing your GUI and before adding components to the GUI? Do the components show up if you resize your GUI?

If so, you'll want to make this call only after your GUI has been fully populated with components, and usually after it has been packed (so that your layout managers can lay out all components and size the GUI correctly).

If this advice doesn't help, then you'll likely need to post some of the offending code. How much to post -- it's hard to say -- enough so that we can identify the problem ;) but not to much as to be drowned in code not related to your problem. The best code to post is an SSCCE if you have the ability to create one.

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