gpt4 book ai didi

java - 在 Java 运行时向面板添加元素

转载 作者:行者123 更新时间:2023-11-29 05:50:02 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How to dynamically add JLabels to JPanel?

我创建了一个面板并为其使用了网格布局。

public pnlFriends() {
initComponents();
this.setLayout(new GridLayout(10,1));
// showFriends();

}

private void formComponentShown(java.awt.event.ComponentEvent evt) {
showFriends();
System.out.print("comp");
}

private void showFriends()
{
//Integer id = NewMain.network.getEdges().get(Main.uuid).getDegree();
for(int i = 0;i < 10 ;i++)
{

String name = "as";
String occupation ="adf";
String place = "asdf";
Integer connections = 5;
this.add(displayMiniProfile(name,occupation,place,connections));
}
}

displayMiniProfile 返回一个 JPanel。

现在,当我在父 JPanel 的构造函数中调用此函数时,它可以工作,我可以添加 MiniProfile JPanel。

但是当我在 componentshown 事件的响应中调用此函数时,它没有显示任何内容。

为什么?

我怎样才能达到同样的效果?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com