Opening a JFrame window by calling an external class

I have a main menu that displays when the program is started.  It is located in the main method and class called mainMenu.java.    If you click the prizeCalculator button on this main menu, it opens the prizeCalculator JFrame window, which is located in an external class called prizeCalculator.java.  
There is a button on the prizeCalculator window that says "main menu" and when clicked, should close the prizeCalculator window and display the mainMenu window again.  

In the prizeCalculator external class, this line of code:
JFrame frame = object1.new mainMenuFrame();  
is giving the error that mainMenuFrame() is not recognized.  

Can you help me show the main menu frame once the "main menu" button is clicked on the prizeCalculator window?    Code and images are attached
 menu.tiff panel.tiff
//From the mainMenu.java file: 

public class mainMenu
{
   public static void main(String[] args)
    {
        JFrame frame = new mainMenuFrame();
        frame.setVisible(true);
    }
}

class mainMenuFrame extends JFrame
{
    public mainMenuFrame()
    {
        setTitle("Main Menu");
        centerWindow(this);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new mainMenuPanel();
        this.add(panel);
        setSize(200, 200);
    }

    private void centerWindow(Window w)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    }
}

class mainMenuPanel extends JPanel implements ActionListener
{

    private JButton     calculatorButton,
                        participantButton,
                        exitButton;
    
    public mainMenuPanel()
    {
       setLayout(new GridBagLayout());

        // display panel
        JPanel displayPanel = new JPanel();
        displayPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
// and so on......


//from the prizeCalculator.java file   This is the part that is telling the main menu button to show the main menu again

// main menu button
        mainMenuButton = new JButton("Main Menu");
        mainMenuButton.addActionListener(
                new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
            mainMenu object1 = new mainMenu();
            JFrame frame = object1.new mainMenuFrame();
            frame.setVisible(true);
            }
        });
        buttonPanel.add(mainMenuButton);

Open in new window

deeayrianAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>             JFrame frame = object1.new mainMenuFrame();

should be:


            JFrame frame = new mainMenuFrame();
0
 
deeayrianAuthor Commented:
Thank you that works perfectly!
But, I have a question.  How come when I needed to show the prizeCalculator JFrame from the main menu window, I had to create a new object, like this:
            prizeCalculator object1 = new prizeCalculator();
            JFrame frame = object1.new prizeCalculatorFrame();
            frame.setVisible(true);

But with this one I don't have to?
0
 
objectsCommented:
because in tht case the JFrame subclass is an inner class, thus you need to create an instance of the outer class
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.