简体   繁体   English

如何在运行时向JPanel添加组件

[英]How do I add components to a JPanel at runtime

I have a JPanel that I want to add some components. 我有一个JPanel ,我想添加一些组件。 in particular JButton s to at runtime based on the content of a user supplied file. 特别是JButton在运行时根据用户提供的文件的内容。

I can add compontents to panel if I call it from the constructor of the JFrame derived form class, even after everything else have been constructed, but If I read the file first and then add components to the panel the call succeds but the added components are never shown. 如果我从JFrame派生表单类的构造函数中调用它,我可以向组件添加组件,即使在构造了其他所有内容之后,但是如果我首先读取文件然后向组件中添加组件,则调用已成功,但添加的组件是从未表现出来

Does anybody know how I force Java to do as I want? 有谁知道我如何强迫Java按我的意愿去做?

在将JButton添加到JPanel之后,在JPanel上调用方法validate()

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

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