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?

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.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

Jens,
With a slight change, my project now works beautifully. Thank you very much for your help. I thought the package statement would eliminate the need for the declarations you suggested, but it didn't.
J.D. Seader

commented: Glad I could help you ;) +1

how would you do this in Netbean IDE? can you show the examples?

let say:

the first screen appear, and it has a series of button. each button goes to specific JFrame... but i have no clues until now.

how to create a window with label and button in java.

DaniWeb Member Rules include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.