| | |
Switching between Multiple Frames
Thread Solved
![]() |
•
•
Join Date: Dec 2007
Posts: 15
Reputation:
Solved Threads: 0
The following four classes are an attempt to set up three different JFrames and switch between them. The three frames are: MainView, DetailView, and FormView. The main class is MultipleFramesExample. The four source codes constitute a NetBeans project with a package called FrameViews. MainView, DetailView, and FormView are essentially identical source codes, each containing a JButton. When a button in one of the views is clicked, you are to be taken to another view.
When I try to Run the project, the last line of each source code creates a compile error, "Cannot find variable". Can anyone tell me how to correct the following four source codes?
_______________________________________
__________________________________________
____________________________________________
_______________________________________
Thank you,
J.D. Seader
When I try to Run the project, the last line of each source code creates a compile error, "Cannot find variable". Can anyone tell me how to correct the following four source codes?
package FrameViews;
import java.awt.*;
import javax.swing.*;
public class MultipleFramesExample extends JFrame {
public MultipleFramesExample() {
}
public void main(String[] args) {
mV.createMainView();
}
}_______________________________________
package FrameViews;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame implements ActionListener {
MainView MV = new MainView();
public void createMainView() {
JFrame frame1 = new JFrame();
frame1.setTitle("Main View");
frame1.setSize(50,50);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
JButton button1 = new JButton("Click for Detail View");
frame1.getContentPane().add(button1);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
dV.createDetailView();
}
}package FrameViews;
import java.awt.event.*;
import javax.swing.*;
public class DetailView extends JFrame implements ActionListener {
DetailView dV = new DetailView();
public void createDetailView() {
JFrame frame2 = new JFrame();
frame2.setTitle("Detail View");
frame2.setSize(100,100);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
JButton button2 = new JButton("Click for Form View");
frame2.getContentPane().add(button2);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
fV.createFormView();
}
}package FrameViews;
import java.awt.event.*;
import javax.swing.*;
public class FormView extends JFrame implements ActionListener {
FormView fV = new FormView();
public void createFormView() {
JFrame frame3 = new JFrame();
frame3.setTitle("Form View");
frame3.setSize(75,75);
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.setVisible(true);
JButton button3 = new JButton("Click for Main View");
frame3.getContentPane().add(button3);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
mV.createMainView();
}
}Thank you,
J.D. Seader
mV.createMainView();
mV has not been declared in this class.
dV.createDetailView();
dV has not been declared in this class.
etc etc.
You should make a new instance for the variables you want to use, or let them get returned from other classes (if you don't want a new one).
So in the first class this would be:
MainView MV = new MainView();
This should solve the problem I think. Please say so if I am wrong.
Greetings, jens
mV has not been declared in this class.
dV.createDetailView();
dV has not been declared in this class.
etc etc.
You should make a new instance for the variables you want to use, or let them get returned from other classes (if you don't want a new one).
So in the first class this would be:
MainView MV = new MainView();
This should solve the problem I think. Please say so if I am wrong.
Greetings, jens
•
•
Join Date: Dec 2007
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
mV.createMainView();
mV has not been declared in this class.
dV.createDetailView();
dV has not been declared in this class.
etc etc.
You should make a new instance for the variables you want to use, or let them get returned from other classes (if you don't want a new one).
So in the first class this would be:
MainView MV = new MainView();
This should solve the problem I think. Please say so if I am wrong.
Greetings, jens
Jens,
Thank you for your help. I followed your suggestion and added a declaration to each class directly after the class declaration. The program now compiles without any errors, but when I run it, no frames with buttons appear. I must have another problem. Can you help me?
J.D. Seader
![]() |
Other Threads in the Java Forum
- Previous Thread: Dumping bulk data from csv file into MS-Access DB.
- Next Thread: Java code packaging for distribusion
| Thread Tools | Search this Thread |
-xlint actionlistener add android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse equation error event fractal ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows xor





