簡體   English   中英

從JFrame關閉JPanel窗口[Java]

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

因此,我想用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將無法與偵聽器一起使用。 我嘗試將偵聽器直接添加到我的游戲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;
    }
}

您在這里創建了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());

您需要重新考慮一下設計,並確保引用了正確的(已經存在的)對象。 也就是說,您在引用現有對象時正在創建對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM