简体   繁体   English

如何使用JMenuItem重置JFrame(重新启动游戏)?

[英]How to reset JFrame (restart game) using a JMenuItem?

I am making a minesweeper, using actionPerformed method (I know how to use it) at any time, I want to reset the game, make a whole new JFrame of the game and closing the old one. 我随时都使用actionPerformed方法(我知道如何使用它)来制作扫雷车,我想重置游戏,制作游戏的全新JFrame并关闭旧的JFrame (the map is generated in JuegoBuca() , by an object from another class). (地图是在JuegoBuca()中由另一个类的对象生成的)。 The game is already working. 游戏已经在工作了。 Also I know how to use the JMenu , the JMenuBar , and the JMenuItem . 我也知道如何使用JMenuJMenuBarJMenuItem

The code of my "main class", where everything takes place. 我的“主类”的代码,在那里发生了一切。

public class JuegoBuca extends JFrame implements MouseListener, ActionListener {

    private JPanel buca, norte;
    private Botones[][] botones;
    private Datos[][] datos;
    private Mapa panel;
    private int numerominas, banderas, minasdescubiertas, clicks, caso;
    private JMenuBar barraMenu;
    private JMenu juego;
    private JMenuItem nuevo, opciones, salir;
    private ImageIcon bandera, bombStar, bombCross, bombHelp, bomb;
    private boolean estado, juegonuevo;

    private JuegoBuca() {

        setVisible(true);
        setSize(250, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Buscaminas");
        setResizable(false);
        //Partes del juego
        panel = new Mapa();
        botones = new Botones[5][5];
        datos = new Datos[5][5];
        buca = new JPanel(new GridLayout(5, 5));
        norte = new JPanel();
        //Variables de información
        banderas = clicks = caso = 0;
        int n = panel.getNumerominas();
        numerominas = n;
        minasdescubiertas = 0;
        estado = juegonuevo =  false;
        //Barra menu y opciones menu
        barraMenu = new JMenuBar();
        juego = new JMenu("Juego");
        //////////////// Jerarquía
        salir = new JMenuItem("Salir");
        salir.addActionListener(this);
        nuevo = new JMenuItem("Nuevo");
        nuevo.addActionListener(this);
        opciones = new JMenuItem("Opciones");
        opciones.addActionListener(this);
        //////////////////////////////////Imagenes
        bandera = new ImageIcon(getClass().getResource("bandera.png"));
        bombStar = new ImageIcon(getClass().getResource("bomb_star.png"));
        bombCross = new ImageIcon(getClass().getResource("bomb_cross.png"));
        bombHelp = new ImageIcon(getClass().getResource("bomb_help.png"));
        bomb = new ImageIcon(getClass().getResource("bomb.png"));

        barraMenu.add(juego);
        juego.add(nuevo);
        juego.add(opciones);
        juego.addSeparator();
        juego.add(salir);
        setJMenuBar(barraMenu);

        norte.add(new JLabel("Clicks " + clicks + "      Minas:  " + numerominas));
        int index = 1;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                buca.add(botones[i][j] = new Botones());
                botones[i][j].addMouseListener(this);
                botones[i][j].setIndice1(index);
                botones[i][j].setIcon(null);
                datos[i][j] = new Datos();
                if (panel.obtenerValorPanel(i, j) == 0) {
                    datos[i][j].ponerLabel(" ");
                } else if (panel.obtenerValorPanel(i, j) == 64) {

                    datos[i][j].setIcon(bombStar);

                } else {
                    datos[i][j].ponerLabel(Integer.toString(panel.obtenerValorPanel(i, j)));
                }


                index++;
            }
        }

        add(buca, BorderLayout.CENTER);
        add(norte, BorderLayout.NORTH);

        actualizar();
    }

    public static void main(String args[]) {

        JuegoBuca juego = new JuegoBuca();
    }

    private void actualizar() {...}

    @Override
    public void mouseClicked(MouseEvent e) {...}

    @Override
    public void mousePressed(MouseEvent e) {...}

    @Override
    public void actionPerformed(ActionEvent e) {
         Object o = e.getSource();
         if (o == salir) {
            System.exit(0);
        } else if(o == nuevo) {

            //RESET GAME...
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {...}

    @Override
    public void mouseEntered(MouseEvent e) {...}

    @Override
    public void mouseExited(MouseEvent e) {...}

    public void indicadores(Object o) {...}

    public void logicaJuego(Object o) {...}

    public void dibuja() {...}

    public int minasdesc() {...}

    public boolean Jugar() {...}


}

If anyone need more info or something, please tell me. 如果有人需要更多信息或其他信息,请告诉我。 Thanks. 谢谢。

Try to do next : 尝试下一步:

JuegoBuca.this.setVisible(false);
JuegoBuca.this.dispose();
new JuegoBuca();

in your : 在你的 :

if(o == nuevo) {
    //RESET GAME...
}

But I recommend you to clear all your resources and reuse existed frame. 但是我建议您清除所有资源并重用现有框架。

Call dispose() method and then call the main() method again. 调用dispose()方法,然后再次调用main()方法。 If you want to do this on clicking any button, do this in actionPerformed() 如果要在单击任何按钮时执行此操作,请在actionPerformed()执行此操作

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

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