简体   繁体   English

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

[英]Java JPanel/GUI contents appear only sometimes when run

i'm new to Java programming, but I've done C++ for a while now. 我是Java编程的新手,但现在已经做过C ++了一段时间。 the whole GUI thing is new to me. 整个GUI东西对我来说都是新的。 i created two JPanels and added them to JFrame using FlowLayout. 我创建了两个JPanel,并使用FlowLayout将它们添加到JFrame。 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. 运行该程序时,我得到的是http://imageshack.us/photo/my-images/43/36213853.jpg/ ,而不是http://imageshack.us/photo/my-images/88/86682510.jpg /仅在某些时候。 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! 谢谢!

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? 没有代码很难回答,但是有机会打包GUI之前以及将组件添加到GUI之前调用setVisible(true)吗? Do the components show up if you resize your GUI? 如果调整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). 如果是这样,则仅 GUI完全填充了组件之后,并且通常在打包后才想进行此调用(以便布局管理器可以布局所有组件并正确调整GUI的大小)。

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. 如果您有能力创建SSCCE ,最好的代码是SSCCE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM