双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

如何在窗体已经建立之后向其中添加控件?

IceCloudLeonhart IceCloudLeonhart 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题,有两个类AddO和AddO1,分别为两个窗体,在AddO1中有个按钮,
我想实现这样的功能:每次按下AddO1中的按钮,AddO中就添加一个按钮控件,应该如何实现?
这是我写的,贴上来,不能实现。。。请教各位大大。。
public class AddO extends JFrame
{
AddO()
{
this.setBounds(100,100,200,400);
this.setLayout(new FlowLayout());
AddO1 a1 = new AddO1(this);
a1.setVisible(true);
}
public static void main(String[] s)
{
AddO a = new AddO();
a.setVisible(true);
}

}

class AddO1 extends JFrame implements ActionListener
{
AddO a;
JButton b;
AddO1(AddO a)
{
this.a = a;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(400,100,200,400);
this.setLayout(new FlowLayout());

b = new JButton("添加");
b.addActionListener(this);
this.add(b);
}

public void actionPerformed(ActionEvent e)
{
//我想在这添加,可这样的代码无法实现功能。。
a.add(new JButton("new"));
a.repaint();
}

}
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
IceCloudLeonhart
Java SE 62,621 成员 307,251 内容