noughts和crosses摆动错误

时间:2017-12-13 11:54:29

标签: java swing

嗨,我正在制作一个简单的Swing noughts并根据教程跨越游戏。但是,当我尝试单击其中一个选项时,我会收到大量错误。我认为它与教程相关,将JButtons更改为JLabel /尝试从JButtons读取文本。或者因为我两次声明变量。但是我不知道如何修复它们。任何帮助,将不胜感激。代码如下!对不起,代码效率不高。我得到的第一个错误是  线程AWT-EventQueue-0“java.lang.NullPointerException

中的异常
public class Game {

    private JFrame frame;
    private String startGame = "X";
    private int Xcount = 0;
    private int Ocount = 0;
    public JLabel btn1;
    public JLabel btn2;
    public JLabel btn3;
    public JLabel btn4;
    public JLabel btn5;
    public JLabel btn6;
    public JLabel btn7;
    public JLabel btn8;
    public JLabel btn9;
    private JTextField txtCountX;
    private JTextField txtCountO;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Game window = new Game();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Game() {
        initialize();
        // gamescore();
    }

    private void WinningGame()
    {
        String b1 = btn1.getText();
        String b2 = btn2.getText();
        String b3 = btn3.getText();
        String b4 = btn4.getText();
        String b5 = btn5.getText();
        String b6 = btn6.getText();
        String b7 = btn7.getText();
        String b8 = btn8.getText();
        String b9 = btn9.getText();
        if(b1.equalsIgnoreCase("X")&&b2.equalsIgnoreCase("X")&&b3.equalsIgnoreCase("X")) {
            JOptionPane.showMessageDialog(frame, "Player X wins","Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
            Xcount++;
            GameScore();
        }
        if(b4.equalsIgnoreCase("X")&&b5.equalsIgnoreCase("X")&&b6.equalsIgnoreCase("X")) {
            JOptionPane.showMessageDialog(frame, "Player X wins","Tic Tac Toe",JOptionPane.INFORMATION_MESSAGE);
            Xcount++;
            GameScore();
        }

    // I have removed the rest of the win statments to keep it short but they are
    // the same as the ones above
    // METHODS////////////////////////////////////////////////
    private void GameScore() {
        txtCountX.setText(String.valueOf(Xcount++));
        txtCountO.setText(String.valueOf(Ocount++));

    }

    private void ChoosePlayer() {
        if (startGame.equalsIgnoreCase("X")) {
            startGame = "O";

        } else {
            startGame = "X";

        }
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1200, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(3, 5, 2, 2));

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_1);
        panel_1.setLayout(new BorderLayout(0, 0));

        JButton btn1 = new JButton("");
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn1.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn1.setForeground(Color.RED);
                } else {
                    btn1.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn1.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_1.add(btn1, BorderLayout.CENTER);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_2);
        panel_2.setLayout(new BorderLayout(0, 0));

        JButton btn2 = new JButton("");
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn2.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn2.setForeground(Color.RED);
                } else {
                    btn2.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn2.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_2.add(btn2, BorderLayout.CENTER);

        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_3);
        panel_3.setLayout(new BorderLayout(0, 0));

        JButton btn3 = new JButton("");
        btn3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn3.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn3.setForeground(Color.RED);
                } else {
                    btn3.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn3.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_3.add(btn3, BorderLayout.CENTER);

        JPanel panel_4 = new JPanel();
        panel_4.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_4);
        panel_4.setLayout(new BorderLayout(0, 0));

        JLabel lblPlayer = new JLabel("Player X:");
        lblPlayer.setFont(new Font("Tahoma", Font.BOLD, 48));
        panel_4.add(lblPlayer, BorderLayout.CENTER);

        JPanel panel_5 = new JPanel();
        panel_5.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_5);
        panel_5.setLayout(new BorderLayout(0, 0));

        txtCountX = new JTextField();
        txtCountX.setHorizontalAlignment(SwingConstants.CENTER);
        txtCountX.setFont(new Font("Tahoma", Font.BOLD, 48));
        txtCountX.setText("0");
        panel_5.add(txtCountX, BorderLayout.CENTER);
        txtCountX.setColumns(10);

        JPanel panel_6 = new JPanel();
        panel_6.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_6);
        panel_6.setLayout(new BorderLayout(0, 0));

        JButton btn4 = new JButton("");
        btn4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn4.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn4.setForeground(Color.RED);
                } else {
                    btn4.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn4.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_6.add(btn4, BorderLayout.CENTER);

        JPanel panel_7 = new JPanel();
        panel_7.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_7);
        panel_7.setLayout(new BorderLayout(0, 0));

        JButton btn5 = new JButton("");
        btn5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn5.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn5.setForeground(Color.RED);
                } else {
                    btn5.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn5.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_7.add(btn5, BorderLayout.CENTER);

        JPanel panel_8 = new JPanel();
        panel_8.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_8);
        panel_8.setLayout(new BorderLayout(0, 0));

        JButton btn6 = new JButton("");
        btn6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn6.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn6.setForeground(Color.RED);
                } else {
                    btn6.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn6.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_8.add(btn6, BorderLayout.CENTER);

        JPanel panel_9 = new JPanel();
        panel_9.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_9);
        panel_9.setLayout(new BorderLayout(0, 0));

        JLabel lblPlayerO = new JLabel("Player O:");
        lblPlayerO.setFont(new Font("Tahoma", Font.BOLD, 48));
        panel_9.add(lblPlayerO, BorderLayout.CENTER);

        JPanel panel_11 = new JPanel();
        panel_11.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_11);
        panel_11.setLayout(new BorderLayout(0, 0));

        txtCountO = new JTextField();
        txtCountO.setHorizontalAlignment(SwingConstants.CENTER);
        txtCountO.setFont(new Font("Tahoma", Font.BOLD, 48));
        txtCountO.setText("0");
        panel_11.add(txtCountO, BorderLayout.CENTER);
        txtCountO.setColumns(10);

        JPanel panel_10 = new JPanel();
        panel_10.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_10);
        panel_10.setLayout(new BorderLayout(0, 0));

        JButton btn7 = new JButton("");
        btn7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn7.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn7.setForeground(Color.RED);
                } else {
                    btn7.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn7.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_10.add(btn7, BorderLayout.CENTER);

        JPanel panel_12 = new JPanel();
        panel_12.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_12);
        panel_12.setLayout(new BorderLayout(0, 0));

        JButton btn8 = new JButton("");
        btn8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn8.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn8.setForeground(Color.RED);
                } else {
                    btn8.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn8.setFont(new Font("Tahoma", Font.BOLD, 96));
        panel_12.add(btn8, BorderLayout.CENTER);

        JPanel panel_13 = new JPanel();
        panel_13.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_13);
        panel_13.setLayout(new BorderLayout(0, 0));

        JButton btn9 = new JButton("");
        btn9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn9.setText(startGame);
                if (startGame.equalsIgnoreCase("x")) {
                    btn9.setForeground(Color.RED);
                } else {
                    btn9.setForeground(Color.BLUE);
                }
                ChoosePlayer();
                WinningGame();
            }
        });
        btn9.setFont(new Font("Dialog", Font.BOLD, 96));
        panel_13.add(btn9, BorderLayout.CENTER);

        JPanel panel_14 = new JPanel();
        panel_14.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_14);
        panel_14.setLayout(new BorderLayout(0, 0));

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btn1.setText(null);
                btn2.setText(null);
                btn3.setText(null);
                btn4.setText(null);
                btn5.setText(null);
                btn6.setText(null);
                btn7.setText(null);
                btn8.setText(null);
                btn9.setText(null);

            }
        });
        btnReset.setFont(new Font("Tahoma", Font.BOLD, 58));
        panel_14.add(btnReset, BorderLayout.CENTER);

        JPanel panel_15 = new JPanel();
        panel_15.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        panel.add(panel_15);
        panel_15.setLayout(new BorderLayout(0, 0));

        JButton btnExit = new JButton("Exit");
        btnExit.setFont(new Font("Tahoma", Font.BOLD, 58));
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame = new JFrame("Exit");
                if (JOptionPane.showConfirmDialog(frame, "Confirm if you want to exit", "Tic Tac Toe",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION) {
                    System.exit(0);
                }
            }
        });
        panel_15.add(btnExit, BorderLayout.CENTER);
    }

}

0 个答案:

没有答案