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

时间:2011-08-16 01:47:26

标签: swing runtime java

菜单在游戏启动时显示并且工作正常,但是一旦在游戏中你可以点击逃生再次调出菜单,这将导致程序最小化。在我最小化游戏后,我可以再次逃脱,菜单屏幕将按预期显示。返回按钮也可以按预期工作。这是怎么回事?

修改 这是我的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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

而不是变体构造函数,让菜单面板进行删除和恢复,如下所示。另请参阅此相关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();
            }
        }
    }
}