【问题标题】:Closing off a JPanel window from a JFrame [Java]从 JFrame [Java] 中关闭 JPanel 窗口
【发布时间】:2015-06-24 20:52:10
【问题描述】:

所以,我想用 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 个帧并且没有正确引用它们。但是现在我解决了这个问题,我的 Listener 出现了问题,当我修复这个问题时,我的 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;
    }
}

【问题讨论】:

    标签: java swing jframe jpanel


    【解决方案1】:

    您在这里创建了一个 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());
    

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

    【讨论】:

    • 谢谢,但现在我已经解决了这个问题,我的 Listener 类将无法工作,而且我不能 100% 确定为什么......
    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 2012-11-05
    • 2021-06-02
    • 2021-11-23
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多