我用的地理布局,这个无所谓了.
每个区域都是个JPanel,现在有个问题,我在右区域的JPanel中有个buttom
我想在按下这个buttom时取其它区域text部件的数据,并在中间区域的JPanel的某个部件接收事件,以完成功能,可我只知道在同类中传递事件,所以程序就卡在了这里,请高手帮忙
小的感激不尽...
每个区域都是个JPanel,现在有个问题,我在右区域的JPanel中有个buttom
我想在按下这个buttom时取其它区域text部件的数据,并在中间区域的JPanel的某个部件接收事件,以完成功能,可我只知道在同类中传递事件,所以程序就卡在了这里,请高手帮忙
小的感激不尽...
比如中间的JPanel中有个JLabel,监听JButton事件,当发生事件后使用label.setText("ABCD");方法将内容显示出来。
你在这个容器里设一个中间的量,作为“桥梁”就可以了啊
那么你就
JPanel1上 set变量 --->JFrame上变量 --->JPanel2上 get变量
不知道你能看懂吗?
在事件响应里将得到的数据用getComp()得到的部件接收其他部件写public:getData()方法,用来得到getComp()返回部件所接受的数据
可以在中间JPanel中写个pulic void setComp(type comp);
public class B extends JPanel{ public void onOk(){
System.out.println("I get it!");
}
}public class C extends JPanel{
private B receiver = null;
public C(B receiver){
this.receiver = receiver;
JButton btnOk = new JButton("OK");
btnOk.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
onOk();
}
}
this.add(btnOk);
} private void onOk(){
receiver.onOK();
}
}