简体   繁体   English

从JFrame关闭JPanel窗口[Java]

[英]Closing off a JPanel window from a JFrame [Java]

So, i want to make a Menu screen with a JPanel, and I got this to work, but when I press the Start button, it does not close the Menu Window, and it just makes a new window, how do I either, keep it on the same window, without closing/opening the menu window, OR I would like to close the menu window and open up the Game window(JPanel), when i press the start button. 因此,我想用JPanel制作一个菜单屏幕,并使它起作用,但是当我按“开始”按钮时,它不会关闭菜单窗口,而只是创建一个新窗口,我该怎么做,保持它在同一窗口上,而无需关闭/打开菜单窗口,或者当我按下开始按钮时,我想关闭菜单窗口并打开游戏窗口(JPanel)。

Here is the MainClass.java 这是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();
    }
}

Here is the Menu.java class 这是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;
            }
        }

    }

EDIT: Thanks to the help of @whiskeyspider I learned that I made 2 frames and did not reference them right. 编辑:感谢@whiskeyspider的帮助,我了解到我做了2帧并且没有正确引用它们。 But now that I got that solved, there is a problem with my Listener, when I fixed this, my Jpanel won't work with my Listener. 但是,现在我解决了这个问题,侦听器出现了问题,当我解决此问题时,我的Jpanel将无法与侦听器一起使用。 I have tried adding the Listener straight to my Game JPanel and to my MainClass JFrame, but neither will work. 我尝试将侦听器直接添加到我的游戏JPanel和MainClass JFrame中,但是都无法正常工作。

Here is some of my Menu class, 这是我的一些菜单课程,

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

You created a MainClass here: 您在这里创建了MainClass:

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

... and again here... ...再来一次...

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

Then when you try to remove the Menu, instead of giving it a reference to the existing Menu, you created a new one: 然后,当您尝试删除菜单时,没有给它提供对现有菜单的引用,而是创建了一个新菜单:

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

You need to rethink your design a little and make sure you are referencing the right (already existing) objects. 您需要重新考虑一下设计,并确保引用了正确的(已经存在的)对象。 That is, you are creating new objects when you be referencing existing ones. 也就是说,您在引用现有对象时正在创建对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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