I got it. Here is the thing. The error you get is this:
Exception in thread "main" java.lang.StackOverflowError
If I remember correctly it usually happens when you run out of memory due some "endless loop" thing. Please someone correct me if I am wrong.
And here is the practical reason why you get an error:
In main you do this: new test0(); You create a new instance of test0. What happens when you do that? : You create a new instance of test1:test1 t1 = new test1();
public static void main(String args[] ){
new test0()
}
What happens when you create a test1 object? :
You create a test0 object
class test1{
test0 t0 = new test0()
....
}
Then the above code calls the constructor of test0 (a NEW instance of test0) therefor a new test1 is created :
class test0{
test1 t1 = new test1();
....
}
And when you created this test1 another test0 is created, then another test1 is created then another test0 is created, .... then you get that overflow thing.
You created an 'endless loop'.
It was like doing this:
public void method1() {
System.out.println("method1");
method2();
}
public void method2() {
System.out.println("method2");
method1();
}
Execute the above code and see what happens
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
What exactly are you trying to accomplish?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Version1:
class Frame1 extends JFrame {
public Frame1() {
}
private void methodClose1_Open2() {
this.setVisible(false);
Frame2 frame2 = new Frame2(this);
frame2.setVisible(true);
}
}
class Frame2 extends JFrame {
Frame1 frame1 = null;
public Frame2(Frame1 frame1) {
this.frame1 = frame1;
}
private void methodClose2_Open1() {
this.setVisible(false);
frame1.setVisisble(true);
}
}
Version 1:
As you can see Frame2 does NOT call a constructor. It uses the 'previous' frame that opened it (frame1).
Frame2 takes as argument a Frame1 object so you need to put the existing frame, not create a new:
Frame2 frame2 = new Frame2(this)
frame2.setVisible(true)
OR Version 2 of Frame1:
class Frame1 extends JFrame {
Frame2 frame2 = null;
public Frame1() {
this.frame2 = new Frame2(this);
}
private void methodClose1_Open2() {
this.setVisible(false);
frame2.setVisible(true);
}
}
Again, the Frame1 constructor will be called ONCE therefor the Frame2 will be called ONCE.
Then you just use the instances to change the visibility of the frames
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Also a second example:
class FrameMaster extends JFrame {
Frame1 frame1;
Frame2 frame2;
// create somehow the 2 frames
public FrameMaster() {
frame1 = new Frame1(this);
frame2 = new Frame2(this);
}
void openFrame1() {
this.setVisible(false);
frame1.setVisible(true);
}
void openFrame2() {
this.setVisible(false);
frame2.setVisible(true);
}
}
class Frame1 extends JFrame {
FrameMaster master;
public Frame1(FrameMaster master) {
this.master = master;
}
void openMasterFrame() {
this.setVisible(false);
master.setVisible(true);
}
}
class Frame2 extends JFrame {
FrameMaster master;
public Frame2(FrameMaster master) {
this.master = master;
}
void openMasterFrame() {
this.setVisible(false);
master.setVisible(true);
}
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448