微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

打开新 JFrame 的按钮

如何解决打开新 JFrame 的按钮

我正在编写一个程序,其中我应该有一个游戏的标题屏幕,您可以在其中单击“播放”按钮,打开另一个窗口并同时关闭一个窗口。我试图做的是将 ActionListener 用于一个按钮,使当前窗口不可见,同时使不同的窗口可见。我很难让它正常工作,并且遇到了很多语法和逻辑错误。我很确定有更好的方法来做到这一点,所以任何指针都将不胜感激。我正在使用摆动。 免责声明:初级Java程序员哈哈:p

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


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

        }
    }
}

我打开另一个框架的另一个包括以下按钮和动作侦听器,我试图在其中从 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 ButtonsHow 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 以了解更多详细信息。

您还应该阅读 JFrameJavaDocs,以便更好地了解该类提供的属性和功能。

看看 How to Make Frames 也无妨。

我建议您查看 How to use CardLayout 而不是尝试隐藏/显示窗口,您通常会获得更好的用户体验。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。