我正在用 Java 制作游戏,每当我尝试调出游戏菜单时,程序就会最小化?

发布于 2024-11-29 21:36:13 字数 3491 浏览 0 评论 0原文

菜单在游戏启动时显示并且工作正常,但是一旦进入游戏,您可以按“Esc”键再次调出菜单,这将导致程序最小化。在我取消最小化游戏后,我可以再次点击退出,菜单屏幕将按预期显示。返回按钮也按预期工作。这是怎么回事?

编辑 这是我的 SSCCE: 您只需要为 BullsEyePanel 添加导入和未实现的方法。我希望这有帮助!

public class Board {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int B_WIDTH = (int) dim.getWidth();
        int B_HEIGHT = (int) dim.getHeight();
        JFrame f = new JFrame("Children of The Ape");
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

        f.setUndecorated(true);
        f.pack();
        f.setBackground(Color.BLACK);
        f.setVisible(true);
        f.setResizable(false);

        // Get graphics configuration...
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        // Change to full screen
        gd.setFullScreenWindow(f);
        if (gd.isDisplayChangeSupported()) {
            gd.setDisplayMode(new DisplayMode(B_WIDTH, B_HEIGHT, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
        }

        MenuPanel menu = new MenuPanel(f);
        f.getContentPane().add(menu);
        f.validate();
    }
}

class BullsEyePanel extends JPanel implements MouseInputListener, ActionListener {

    JFrame frame;

    public BullsEyePanel(JFrame f) {
        frame = f;
        addKeyListener(new TAdapter());
        setFocusable(true);
        setVisible(true);
        repaint();
    }

    private void openMenu() {
        frame.getContentPane().add(new MenuPanel(this));
        setVisible(false);
    }

    private class TAdapter extends KeyAdapter {

        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == 27) {
                openMenu();
            }
        }
    }
}

class MenuPanel extends JPanel {

    JButton btnExit;
    JButton btnNewGame;
    JFrame f;
    BullsEyePanel panel;

    MenuPanel(JFrame frame) { //this menu constructor is only called on program startup
        f = frame;
        setBackground(Color.black);
        setFocusable(true);

        btnNewGame = new JButton("New Game");
        btnExit = new JButton("Exit");
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        add(btnNewGame);
        add(btnExit);

        setVisible(true);
    }

    MenuPanel(BullsEyePanel bullsEyePanel) { //this menu constructor is called when ESC is typed
        f = bullsEyePanel.frame;
        setBackground(Color.black);
        setFocusable(true);

        btnNewGame = new JButton("New Game");
        btnExit = new JButton("Exit");
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        add(btnNewGame);
        add(btnExit);

        setVisible(true);
    }

    public class exitListener implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    }

    public class newGameListener implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            setVisible(false);
            f.getContentPane().add(new BullsEyePanel(f), BorderLayout.CENTER);
        }
    }
}

The menu is displayed on game startup and works fine, but once in game you can hit escape to bring up the menu again and this will cause the program to minimize. After I unminimize the game I can hit escape again and the menu screen will appear as intended. The return button also works as intended. What is going on here?

EDIT
Here is my SSCCE:
You will just need to add the imports and unimplemented methods for BullsEyePanel. I hope this helps!

public class Board {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int B_WIDTH = (int) dim.getWidth();
        int B_HEIGHT = (int) dim.getHeight();
        JFrame f = new JFrame("Children of The Ape");
        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);

        f.setUndecorated(true);
        f.pack();
        f.setBackground(Color.BLACK);
        f.setVisible(true);
        f.setResizable(false);

        // Get graphics configuration...
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        // Change to full screen
        gd.setFullScreenWindow(f);
        if (gd.isDisplayChangeSupported()) {
            gd.setDisplayMode(new DisplayMode(B_WIDTH, B_HEIGHT, 32, DisplayMode.REFRESH_RATE_UNKNOWN));
        }

        MenuPanel menu = new MenuPanel(f);
        f.getContentPane().add(menu);
        f.validate();
    }
}

class BullsEyePanel extends JPanel implements MouseInputListener, ActionListener {

    JFrame frame;

    public BullsEyePanel(JFrame f) {
        frame = f;
        addKeyListener(new TAdapter());
        setFocusable(true);
        setVisible(true);
        repaint();
    }

    private void openMenu() {
        frame.getContentPane().add(new MenuPanel(this));
        setVisible(false);
    }

    private class TAdapter extends KeyAdapter {

        public void keyPressed(KeyEvent arg0) {
            if (arg0.getKeyCode() == 27) {
                openMenu();
            }
        }
    }
}

class MenuPanel extends JPanel {

    JButton btnExit;
    JButton btnNewGame;
    JFrame f;
    BullsEyePanel panel;

    MenuPanel(JFrame frame) { //this menu constructor is only called on program startup
        f = frame;
        setBackground(Color.black);
        setFocusable(true);

        btnNewGame = new JButton("New Game");
        btnExit = new JButton("Exit");
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        add(btnNewGame);
        add(btnExit);

        setVisible(true);
    }

    MenuPanel(BullsEyePanel bullsEyePanel) { //this menu constructor is called when ESC is typed
        f = bullsEyePanel.frame;
        setBackground(Color.black);
        setFocusable(true);

        btnNewGame = new JButton("New Game");
        btnExit = new JButton("Exit");
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        add(btnNewGame);
        add(btnExit);

        setVisible(true);
    }

    public class exitListener implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    }

    public class newGameListener implements ActionListener {

        public void actionPerformed(ActionEvent arg0) {
            setVisible(false);
            f.getContentPane().add(new BullsEyePanel(f), BorderLayout.CENTER);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-12-06 21:36:13

让菜单面板进行删除和恢复,而不是使用变体构造函数,如下所示。另请参阅此相关示例

在此处输入图像描述
在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Board {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Board().createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        JFrame f = new JFrame("Children of The Board");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(Color.BLACK);
        MenuPanel menu = new MenuPanel(f);
        f.add(menu, BorderLayout.NORTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class MenuPanel extends JPanel {

    private JButton btnExit = new JButton("Exit");
    private JButton btnNewGame = new JButton("New Game");
    private BullsEyePanel gamePanel;
    private JFrame parent;

    MenuPanel(JFrame parent) {
        this.gamePanel = new BullsEyePanel(this);
        this.parent = parent;
        this.setBackground(Color.black);
        this.setFocusable(true);
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        this.add(btnNewGame);
        this.add(btnExit);
        this.setVisible(true);
    }

    public void restore() {
        parent.remove(gamePanel);
        parent.add(this, BorderLayout.NORTH);
        parent.pack();
        parent.setLocationRelativeTo(null);
    }

    private class exitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    }

    private class newGameListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            parent.remove(MenuPanel.this);
            parent.add(gamePanel, BorderLayout.CENTER);
            parent.pack();
            parent.setLocationRelativeTo(null);
            gamePanel.requestFocus();
        }
    }
}

class BullsEyePanel extends JPanel {

    private MenuPanel menuPanel;

    public BullsEyePanel(MenuPanel menu) {
        this.menuPanel = menu;
        this.setFocusable(true);
        this.addKeyListener(new TAdapter());
        this.setPreferredSize(new Dimension(320, 240)); // placeholder
        this.setVisible(true);
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent code) {
            if (code.getKeyCode() == KeyEvent.VK_ESCAPE) {
                menuPanel.restore();
            }
        }
    }
}

Instead of variant constructors, let the menu panel undertake its removal and restoration, as suggested below. See also this related example.

enter image description here
enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Board {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Board().createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI() {
        JFrame f = new JFrame("Children of The Board");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(Color.BLACK);
        MenuPanel menu = new MenuPanel(f);
        f.add(menu, BorderLayout.NORTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class MenuPanel extends JPanel {

    private JButton btnExit = new JButton("Exit");
    private JButton btnNewGame = new JButton("New Game");
    private BullsEyePanel gamePanel;
    private JFrame parent;

    MenuPanel(JFrame parent) {
        this.gamePanel = new BullsEyePanel(this);
        this.parent = parent;
        this.setBackground(Color.black);
        this.setFocusable(true);
        btnNewGame.addActionListener(new newGameListener());
        btnExit.addActionListener(new exitListener());
        this.add(btnNewGame);
        this.add(btnExit);
        this.setVisible(true);
    }

    public void restore() {
        parent.remove(gamePanel);
        parent.add(this, BorderLayout.NORTH);
        parent.pack();
        parent.setLocationRelativeTo(null);
    }

    private class exitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.exit(0);
        }
    }

    private class newGameListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            parent.remove(MenuPanel.this);
            parent.add(gamePanel, BorderLayout.CENTER);
            parent.pack();
            parent.setLocationRelativeTo(null);
            gamePanel.requestFocus();
        }
    }
}

class BullsEyePanel extends JPanel {

    private MenuPanel menuPanel;

    public BullsEyePanel(MenuPanel menu) {
        this.menuPanel = menu;
        this.setFocusable(true);
        this.addKeyListener(new TAdapter());
        this.setPreferredSize(new Dimension(320, 240)); // placeholder
        this.setVisible(true);
    }

    private class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent code) {
            if (code.getKeyCode() == KeyEvent.VK_ESCAPE) {
                menuPanel.restore();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文