在游戏开始前添加菜单

时间:2017-04-22 21:14:28

标签: java swing

由于某些原因,添加菜单后我的jFrame不再弹出。有什么我想念的吗?我试图制作一个在游戏开始之前弹出的菜单,并且有按钮" play"以及允许用户输入用户名的文本框。

有关如何修复代码的任何建议?谢谢!

这是我的菜单类:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;

    import javax.swing.JButton;
    import javax.swing.JPanel;

    public class Menu extends JPanel{

        private static final long serialVersionUID = 1L;

        public Menu() {

            JPanel buttonPanel = new JPanel(new GridLayout());

            JButton play = new JButton();   
            JButton help = new JButton();
            buttonPanel.add(play);
            buttonPanel.add(help);

            setFocusable(true);

            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    Game.started = true;
                }
            });
        }

        public void paint (Graphics g) {    
            super.paint(g);
            g.setColor(Color.black);
            g.fillRect(400, 400, Game.WIDTH, Game.HEIGHT);  
        }

    }

这是我运行程序的Main类:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main implements Runnable{
    public void run() {
    final JFrame frame = new JFrame();

    frame.setTitle("Flying Square");
    frame.setSize(Game.WIDTH, Game.HEIGHT);

//The menu
    final Menu menu = new Menu();
    final Game game = new Game();
    frame.add(menu, BorderLayout.CENTER);
    menu.setVisible(true);

    try {while (Game.started == false) {
        Thread.sleep(10);
    }} catch (InterruptedException e){
        e.printStackTrace();
    }

    frame.remove(menu);

 //Main playing area
    frame.add(game, BorderLayout.CENTER);
    game.setVisible(true);
    frame.revalidate();

 // Put the frame on the screen
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

 // add listeners
    frame.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            flyingObject.jump();
        }
    });

    frame.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {

        }

        @Override
        public void keyTyped(KeyEvent e) {


        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE)
            {
                flyingObject.jump();
            }

        }
    });

 // Start game
    Game.reset();
}

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Main());
    }
}

1 个答案:

答案 0 :(得分:1)

所以,这......

try {while (Game.started == false) {
    Thread.sleep(10);
}} catch (InterruptedException e){
    e.printStackTrace();
}

阻止事件调度线程,阻止它处理任何事件并导致程序挂起。

这不是您希望如何处理用户的响应。你的Menu应该监视来自用户的输入,可能是通过一个或多个ActionListener,当一个动作被触发时,它应该通知某种控制器,然后控制器可以做出关于什么的决定它需要做,例如切换面板

你想要将你的代码分解成至少三个块,即#34;游戏" "菜单"和#34;控制器",这样管理起来会更容易,而不是试图将功能改进现有代码

建议看看