如何在窗体已经建立之后向其中添加控件?
如题,有两个类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();
}
}
我想实现这样的功能:每次按下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();
}
}
...全文
相关推荐
C#中用代码在窗体上任意添加控件
在窗体上双击任意位置创建文本框控件,并要求文本框中显示序号,在窗体上右键单击,在鼠标点击位置创建ComboBox,并要求为每个ComboBox自动添加选项
C#如何动态添加或删除窗体中的控件(源码示例)
操作程序窗体的controls属性实现动态添加和删除程序窗体中的控件。
C#与halcon窗体控件交互
C#与halcon窗体控件交互,可以使用。可以供给大家看看,也是从别处得到的。 C#与halcon窗体控件交互,可以使用。可以供给大家看看,也是从别处得到的
窗体控件大小随窗体的大小而自动变化
本代码能够实现窗体中的控件随窗体大小的改变而自动改变,任意改变窗体大小,相关控件大小随之变化。
C#中主窗体Panel中加载其他多个窗体Panel控件
提供了一个C#中主窗体Panel中加载其他多个窗体Panel控件的办法


Java SE

