单击JMenu时创建一个新游戏

时间:2021-01-30 20:43:32

I have a main class who hold the frame and the JMenu, and another class for the game extending from JPanel. How is possible for when is clicked one option from jMenu create a new game?

我有一个持有框架和JMenu的主类,以及从JPanel扩展的另一个游戏类。如何从jMenu点击一个选项创建一个新游戏?

At the moment, I have this

目前,我有这个

class Application {

    private static Game game;
    private static TheFrame frame;
    private static JMenuBar menu;

    public Application(){
        frame = new TheFrame();
        menu = new JMenuBar();
        JMenu file = new JMenu("File");
        menu.add(file);
        frame.setJMenuBar(menu);
        JMenuItem newGame = new JMenuItem("New Game");
        newGame.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                newGame();
            }
        });
        file.add(newGame);
        JMenuItem scores = new JMenuItem("Show Scores");
        scores.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new Score();
            }
        });
        file.add(scores);
        JMenuItem exit = new JMenuItem("Exit!");
        exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        file.add(exit);
    }

    private void newGame(){
//      frame.getContentPane().removeAll();
        frame.getContentPane().setBackground(Color.pink);
        game = new Game();
        frame.addGameToCanvas(game);
        game.runMenu();
    }

    /**
     * Main method
     * @param args
     */
    public static void main(String args[]) {
        Application app = new Application();
    }
}

But when I click on new Game, the frame get black and doesn't show anything. I had before this on the command line, and all work fine, but when I try to do this using the menu doesn't work. Any suggestions?

但是当我点击新游戏时,框架变黑并且没有显示任何内容。我在命令行之前有这个,并且一切正常,但是当我尝试使用菜单时这样做不起作用。有什么建议?

EDIT

the code for frame.addGametoCanva(). What it does, is add the game object to a canvas. public void addGameToCanvas(Game g){ canvas = g; add(canvas, BorderLayout.CENTER); invalidate(); validate(); repaint(); }

frame.addGametoCanva()的代码。它的作用是将游戏对象添加到画布中。 public void addGameToCanvas(Game g){canvas = g; add(canvas,BorderLayout.CENTER);无效();验证();重绘(); }

And yes, the game object, have a finite loop, for the user inputs (from console)

是的,游戏对象有一个有限的循环,用于用户输入(来自控制台)

1 个解决方案

#1


the game object, have a finite loop, for the user inputs (from console)

游戏对象,有一个有限的循环,为用户输入(从控制台)

The Event-Dispatch thread (EDT) is single threaded - it takes care of painting, event handling, etc... If one attempts to perform a long running tasks on this thread, like waiting for user input from a different source (like the console) the EDT can lock up (in other words an unresponsive UI).

Event-Dispatch线程(EDT)是单线程的 - 它负责绘画,事件处理等......如果有人试图在这个线程上执行长时间运行的任务,比如等待来自不同来源的用户输入(比如控制台)EDT可以锁定(换句话说,无响应的用户界面)。

Any long running tasks should be placed inside its own Thread or ran via a SwingWorker. Further, most calls on a non-EDT thread should be dispatched to the EDT using SwingUtilities.invokeLater or SwingUtilities.invokeAndWait (this includes construction of the UI - note the main method operates on a non-EDT thread).

任何长时间运行的任务都应该放在自己的Thread中,或者通过SwingWorker运行。此外,应该使用SwingUtilities.invokeLater或SwingUtilities.invokeAndWait将非EDT线程上的大多数调用分派给EDT(这包括构建UI - 注意主方法在非EDT线程上运行)。

Although, I'm not entirely sure why the mix of UI (eg Console and Swing) - you might consider choosing just a single UI.

虽然,我不完全确定为什么混合使用UI(例如Console和Swing) - 您可能会考虑只选择一个UI。

#1


the game object, have a finite loop, for the user inputs (from console)

游戏对象,有一个有限的循环,为用户输入(从控制台)

The Event-Dispatch thread (EDT) is single threaded - it takes care of painting, event handling, etc... If one attempts to perform a long running tasks on this thread, like waiting for user input from a different source (like the console) the EDT can lock up (in other words an unresponsive UI).

Event-Dispatch线程(EDT)是单线程的 - 它负责绘画,事件处理等......如果有人试图在这个线程上执行长时间运行的任务,比如等待来自不同来源的用户输入(比如控制台)EDT可以锁定(换句话说,无响应的用户界面)。

Any long running tasks should be placed inside its own Thread or ran via a SwingWorker. Further, most calls on a non-EDT thread should be dispatched to the EDT using SwingUtilities.invokeLater or SwingUtilities.invokeAndWait (this includes construction of the UI - note the main method operates on a non-EDT thread).

任何长时间运行的任务都应该放在自己的Thread中,或者通过SwingWorker运行。此外,应该使用SwingUtilities.invokeLater或SwingUtilities.invokeAndWait将非EDT线程上的大多数调用分派给EDT(这包括构建UI - 注意主方法在非EDT线程上运行)。

Although, I'm not entirely sure why the mix of UI (eg Console and Swing) - you might consider choosing just a single UI.

虽然,我不完全确定为什么混合使用UI(例如Console和Swing) - 您可能会考虑只选择一个UI。