打开新 JFrame 的按钮

Button that opens a new JFrame

我正在编写一个程序,其中我应该有一个游戏的标题屏幕,您可以在其中单击一个 'play' 按钮打开一个不同的 window 并同时关闭另一个.我正在尝试做的是将 ActionListener 用于使当前 window 不可见的按钮,同时使不同的 window 可见。我很难让它工作并且遇到很多语法和逻辑错误。我很确定有更好的方法可以做到这一点,因此将不胜感激任何指点。我正在使用摆动。 免责声明:初学者 java 级程序员 lol :p

我的第一个 class 包括这个(不包括我的进口):


    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);

        }
    }
}

我的另一个 class 打开了另一个框架,包括以下按钮和动作侦听器,我试图从 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);
            }
        }
    }
}

你应该先看看 How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener

ActionListener应该被注册到按钮上,所以如果发生某些动作时可以得到通知,例如...

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");            
        }

    }
}

要更改对象,您首先需要对该对象的引用。这是非常基本的 Java 101 种东西,您应该查看 Passing Information to a Method or a Constructor 了解更多详细信息。

您还应该阅读 JavaDocs for JFrame 以更好地了解 class 提供的属性和功能。

看看 How to Make Frames 也没什么坏处。

我建议您查看 How to use CardLayout 而不是尝试 hide/show windows,总体上您将获得更好的用户体验。