there are several ways to do that...for example, you can build the first frame and then use the first frame as the parameter of the second frame.Then you can write getXXX() or setXXX() methods to access the variables of the other frame. That helps information encapsulation. Don't access those variables directly by frame.XXX. That's not a good habit.
That may not be the best way in code efficiency but likely the most readable program architecture.
比如在frame2的对象里面看到frame1的东西:
class frame1{
main(){
......
frame2 vv=new frame2;
vv.yy=this;
........}
}class frame2{
frame1 yy;
}
1.构造函数
2.static
只好在说一遍啦。
你可以用getXXX()和setXXX()方法来访问另外一个类的变量。
譬如说,那个类有一个JTextField类的field变量。那么写public JTextField getField()
{
return this.field;
}public void setField(JTextField field)
{
this.field = field;
}然后把这个类的一个实例作为参数传递给第二个类的实例;
假使第二个类的构造方法是这样的。
public Frame2(Frame frame1)
{
this.frame = frame1;
}
那么在这个第二个类的实例中就可以这样访问第一个类的field变量。
JTextField field2 = frame.getField();
or
frame.setField(new JTextField("另外一个TextField"));如果涉及到事件侦听的话,最好采用EventListener接口的办法,这样的话,可以使结构更加***。不过现在就不用考虑了。