简体   繁体   中英

Button that opens a new JFrame

I'm writing a program where I am supposed to have a title screen for a game, where you can click a 'play' button that opens a different window and simultaneously closes the other one. What I am attempting to do is utilize an ActionListener for a button that makes the current window invisible while simultaneously making a different window visible. I am having a hard time getting this to work and am encountering a lot of syntax and logical errors. I'm quite certain there is a better way to do this, so any pointers will be greatly appreciated. I am using swing. Disclaimer: beginner java level programmer lol:p

My first class includes this (does not include my imports):


    public static void main(String[] args) {

        //Creating the Frame
        MyFrame menuFrame = new MyFrame();

    // (here i have code for the frame in my actual program)
    }
    static class MyFrame extends JFrame {

        MyFrame () {

            this.setDefaultCloseOperation
                    (JFrame.EXIT_ON_CLOSE);

            this.getContentPane().add(new JLabel(new ImageIcon("logo.png")));
            this.pack();
            this.setVisible(true);

            new EightOff.returnHomeListener (this);

        }
    }
}

My other class that has the other frame being opened includes the following button and action listener where I am trying to reference my frame from GameMenu :

public class EightOff
{
private static JButton returnHome = new JButton("Return to Game Menu");

public static class returnHomeListener implements ActionListener {
        public returnHomeListener(GameMenu.MyFrame myFrame) {

        }

        @Override
        public void actionPerformed(ActionEvent e)
            {
                returnHomeListener (JFrame visibleFrame) {
                visibleFrame.toSetVisible (true);
            }
        }
    }
}

You should start by having a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener

The ActionListener should be registered to the button, so if can be notified when some action occurs, for example...

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JButton button = new JButton("Click me");
                button.addActionListener(new TestActionListener());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(button);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "I'd prfefer if you did't do that");            
        }

    }
}

In order to make changes to a object, you first need a reference to that object. This is pretty basic, Java 101 kind of stuff and you should have a look at Passing Information to a Method or a Constructor for more details.

You should also have a read of the JavaDocs for JFrame to get a better understanding of what properties and functionality the class provides.

Having a look at How to Make Frames also wouldn't hurt.

I'd recommend having a look at How to use CardLayout instead of trying to hide/show windows, you'll have a better user experience generally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM