如何使用JMenuBar开始新游戏

时间:2018-01-16 22:16:23

标签: java swing jmenubar

我有一个带球的游戏,我试图创建JMenuBar

按钮New出现问题。现在,当我运行程序时,游戏立即开始,当我点击按钮New时,游戏将在新窗口中再次打开。我对此方法有疑问:fileNew.addActionListener(new ActionListener()。此方法在新窗口中再次运行游戏,它不正确。

我的目标是:当我首先运行程序时,我会看到空白表,只有在单击按钮New后才会启动它(出现球)。如果我在游戏过程中单击按钮New,它将在同一窗口中再次启动。我怎样才能做到这一点?

enter image description here

我的代码:

package api;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;
import gui.Render;
import database.SaveGame;

public class Main extends JFrame {

public Main() {

    // ------menu------//
    JMenuBar menubar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    JMenuItem fileNew = new JMenuItem("New");
    fileNew.setMnemonic(KeyEvent.VK_N);
    JMenuItem fileSave = new JMenuItem("Save");
    fileNew.setMnemonic(KeyEvent.VK_O);
    JMenuItem fileLoad = new JMenuItem("Load");
    fileSave.setMnemonic(KeyEvent.VK_S);

    JMenuItem fileExit = new JMenuItem("Exit");
    fileExit.setMnemonic(KeyEvent.VK_C);
    fileExit.setToolTipText("Exit application");
    fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

    JMenu helpMenu = new JMenu("Help");
    JMenuItem about = new JMenuItem("About");

       // website button action
    about.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            launchURL("https://www.kurnik.pl/kulki/zasady.phtml");
        }
    });

    // ---new---//
    fileNew.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            String[] value = null;
            Main.main(value);
                }
    });

    // ---exit---//
    fileExit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    fileMenu.add(fileNew); // file
    fileMenu.add(fileLoad); // load
    fileMenu.add(fileSave); // save
    fileMenu.addSeparator(); // ____
    fileMenu.addSeparator(); // ____
    fileMenu.add(fileExit); // exit
    helpMenu.add(about);

    menubar.add(fileMenu);
    menubar.add(helpMenu);

    setJMenuBar(menubar);

    int width = Game.widthSize * Render.SCALE;
    int height = Game.heightSize * Render.SCALE;
    Game game = new Game();
    add(new Render(game, width, height));

    setTitle("Kulki");
    setSize(width + 18, height + 38);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

static void launchURL(String s) {
    String s1 = System.getProperty("os.name");
    try {

        if (s1.startsWith("Windows")) {
            Runtime.getRuntime().exec((new StringBuilder()).append("rundll32 url.dll,FileProtocolHandler ").append(s).toString());
        } else {
            String as[] = {"firefox", "opera", "konqueror", "epiphany",
                "mozilla", "netscape"};
            String s2 = null;
            for (int i = 0; i < as.length && s2 == null; i++) {
                if (Runtime.getRuntime().exec(new String[]{"which", as[i]}).waitFor() == 0) {
                    s2 = as[i];
                }
            }

            if (s2 == null) {
                throw new Exception("Could not find web browser");
            }
            Runtime.getRuntime().exec(new String[]{s2, s});
        }
    } catch (Exception exception) {
        System.out.println("An error occured while trying to open the web browser!\n");
    }
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            Main ms = new Main();
            ms.setVisible(true);
        }
    });
}
}

0 个答案:

没有答案