1

重複の可能性:
JLabelを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のコンストラクターでこの関数を呼び出すと、この関数が機能し、MiniProfileJPanelを追加できます。

しかし、componentshownイベントの応答でこの関数を呼び出すと、何も表示されません。

なぜ ?

そして、どうすれば同じことを達成できますか?

4

1 に答える 1

3

JPanel新しいパネルが追加された後もまだ検証されていないため、何も表示されません。電話する必要があります

revalidate();
repaint();

電話した後

this.add(displayMiniProfile(name,occupation,place,connections));
于 2013-01-10T23:31:16.110 に答える