java panel添加控件_java – 向JPanel动态添加组件

我正在尝试动态地将一些组件添加到JPanel,但不幸的是它们没有出现.我只看到构造函数中添加的那些.

更新版本(添加新的JPanel,其中所有组件都将是):

public class View extends JPanel {

JPanel panel = new JPanel();

JLabel label;

JLabel labels[];

JButton b1 = new JButton("OK");

public View() {

this.setLayout(new FlowLayout());

this.add(panel); // adding a new JPanel

label = new JLabel("My label");

panel.add(label); // adding label to the new panel (this one works)

}

public void showLabels() {

System.out.println("function showLabels called");

labels = new JLabel[5];

for (int i = 0; i < 5; i++) {

labels[i] = new JLabel("Label: " + i);

panel.add(labels[i]); // this one doesn't work

}

panel.add(b1); // this one doesn't work, too

this.validate(); // validating this class (parent container)

panel.validate(); // validating the panel, where all the components are

}

}

不幸的是没有任何改变.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值