首页 文章

从JFrame [Java]关闭JPanel窗口

提问于
浏览
0

所以,我想制作一个带有JPanel的菜单屏幕,我让它工作,但是当我按下“开始”按钮时,它没有关闭菜单窗口,它只是打开一个新窗口,我该怎么做,保持它在同一个窗口,没有关闭/打开菜单窗口,或者我想关闭菜单窗口并打开游戏窗口(JPanel),当我按下开始按钮 .

这是MainClass.java

package bombermangame;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JFrame{
    private static final long serialVersionUID = 1L;

    public static int WIDTH = 870, HEIGHT = 800;
    public static JPanel menu = new Menu();
    public static Listener keys = new Listener();

    public MainClass(){
        setContentPane(menu);
        pack();
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("BomberMan V0.3");
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
        setVisible(true);
    }

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

这是Menu.java类

package bombermangame;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
    import java.util.TimerTask;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Menu extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JButton startButton = new JButton("Play");

        private int x = 0, y = 500;

        private boolean down = false;
        private boolean up = true;

        private Timer timer = new Timer();

        public Menu() {
            setBackground(Color.blue);
            startButton = new JButton("Start");
            startButton.setBounds(0,0, 100, 40);
            startButton.setPreferredSize(new Dimension(100, 40));
            startButton.addActionListener(this);
            startButton.setFocusPainted(true);
            this.add(startButton);


        public void actionPerformed(ActionEvent ae) {
            Object a = ae.getSource();
            Game game = new Game();
            MainClass frm = new MainClass();
            Listener keys = new Listener();

            if (a == startButton) {
                timer.cancel();
                frm.getContentPane().remove(new Menu());
                frm.addKeyListener(keys);
                frm.setContentPane(game);
                frm.revalidate();
                frm.repaint();
                game.setBackground(Color.BLACK);
                game.setDoubleBuffered(true);
                game.setBounds(0, 0, WIDTH, HEIGHT);
                Game.running = true;
            }
        }

    }

编辑:感谢@whiskeyspider的帮助,我了解到我制作了2帧并没有正确引用它们 . 但是现在我已经解决了,我的监听器出现了问题,当我修复它时,我的Jpanel将无法使用我的监听器 . 我已经尝试将Listener直接添加到我的Game JPanel和我的MainClass JFrame中,但两者都不会起作用 .

这是我的一些菜单类,

public void actionPerformed(ActionEvent ae) {
    Object a = ae.getSource();
    JPanel game = new Game();
    Listener keys = new Listener();

    if (a == startButton) {
        timer.cancel();
        MainClass.frame.getContentPane().remove(this);
        MainClass.frame.setContentPane(game);
        MainClass.frame.addKeyListener(keys);
        game.addKeyListener(keys);
        game.setBackground(Color.BLACK);
        game.setDoubleBuffered(true);
        game.setBounds(0, 0, WIDTH, HEIGHT);
        Game.running = true;
    }
}

1 回答

  • 2

    你在这里创建了一个MainClass:

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

    ......再来一次......

    public void actionPerformed(ActionEvent ae) {
        Object a = ae.getSource();
        JPanel game = new Game();
        JFrame frm = new MainClass();
    

    然后,当您尝试删除菜单时,您创建了一个新菜单,而不是为现有菜单提供引用:

    frm.getContentPane().remove(new Menu());
    

    您需要重新考虑一下您的设计,并确保引用正确的(已存在的)对象 . 也就是说,当您引用 existing 时,您正在创建 new 对象 .

相关问题