Java - Tic Tac Toe Swing 游戏 - 错误 [英] Java - Tic Tac Toe Swing game - Errors

查看:28
本文介绍了Java - Tic Tac Toe Swing 游戏 - 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 制作井字游戏并制作了 gui 并想运行它来测试它,但遇到了一些错误.我不知道为什么,并希望有人能解释为什么会出现这些错误以及我应该怎么做来修复它们.我得到的错误如下:线程main"中的异常 java.lang.NullPointerException在 TicTacToeSwing.(TicTacToeSwing.java:84)在 TicTacToeSwing.main(TicTacToeSwing.java:180)

I am making a tic tac toe game in Java and made the gui and wanted to run it to test it but am getting a few errors. I am not sure why and was hoping that someone could explain why those errors are coming up and what i should do to fix them. The errors that I am getting are as follows: Exception in thread "main" java.lang.NullPointerException at TicTacToeSwing.(TicTacToeSwing.java:84) at TicTacToeSwing.main(TicTacToeSwing.java:180)

这是我的代码:(请注意:我还没有完成计算,因为我想先运行 gui)如果您知道我应该考虑做的更好的实践,请务必告诉我.

Here is my code: (please note: I have not done the calculations yet because I wanted to get the gui going first) If you know of a better practice that I should consider doing, by all means, let me know.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

//井字游戏

    public class TicTacToeSwing extends JFrame
    implements ActionListener { 

//JButtons

//private JButton button1 = new JButton("");    
private JButton jbtnTicTacToe1;
private JButton jbtnTicTacToe2;
private JButton jbtnTicTacToe3;
private JButton jbtnTicTacToe4;
private JButton jbtnTicTacToe5;
private JButton jbtnTicTacToe6;
private JButton jbtnTicTacToe7;
private JButton jbtnTicTacToe8;
private JButton jbtnTicTacToe9;

private JButton jbtnExit;
private JButton jbtnReset;

//JFrame window = new JFrame("Tic-Tac-Toe Swing ");
private JFrame window = new JFrame("Tic-Tac-Toe");


//labels
private JLabel jlblPlayerX = new JLabel ("X");
private JLabel jlblPlayerO = new JLabel ("O");

//text fields
JTextField jtfName = new JTextField(20);
private JTextField jtfPlayerX = new JTextField("X");
private JTextField jtfPlayerO = new JTextField("O");

//Panels
JPanel jpnlMain = new JPanel ();
    JPanel jpnlFamily = new JPanel();
    JPanel jpnlNorth = new JPanel();
    JPanel jpnlSouth = new JPanel();
    JPanel jpnlCenter = new JPanel();
    JPanel jpnlTop = new JPanel();
    JPanel jpnlBottom = new JPanel();

//Class Constructor
public TicTacToeSwing () {

    //Prepare JFrame/Window
    super ("Tic Tac Toe");
    setSize(400,400);
    setTitle("Tic Tac Toe Swing");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set the layouts for 3 rows and 3 columns
    jpnlMain.setLayout(new BorderLayout(3,3));
    jpnlCenter.setLayout(new GridLayout());
    jpnlSouth.setLayout(new GridLayout());
    jpnlTop.setLayout(new BorderLayout());
    jpnlBottom.setLayout(new BorderLayout());

    //Center Panel
    jpnlCenter.add(jlblPlayerX);
    jpnlCenter.add(jlblPlayerO);

    //identify each JButton
    jbtnReset.setActionCommand("Reset");
    jbtnExit.setActionCommand("Exit");

    //register JButton for event handling by using the THIS keyword - THIS class will handle the events
    jbtnReset.addActionListener(this);
    jbtnExit.addActionListener(this);

    /*Add Buttons To The Window*/
    window.add(jbtnTicTacToe1);
    window.add(jbtnTicTacToe2);
    window.add(jbtnTicTacToe3);
    window.add(jbtnTicTacToe4);
    window.add(jbtnTicTacToe5);
    window.add(jbtnTicTacToe6);
    window.add(jbtnTicTacToe7);
    window.add(jbtnTicTacToe8);
    window.add(jbtnTicTacToe9);

    /*Add The Action Listener To The Buttons
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);
    */

    jbtnTicTacToe1.addActionListener(this);
    jbtnTicTacToe2.addActionListener(this);
    jbtnTicTacToe3.addActionListener(this);
    jbtnTicTacToe4.addActionListener(this);
    jbtnTicTacToe5.addActionListener(this);
    jbtnTicTacToe6.addActionListener(this);
    jbtnTicTacToe7.addActionListener(this);
    jbtnTicTacToe8.addActionListener(this);
    jbtnTicTacToe9.addActionListener(this);

    //South Button Panel
    jpnlSouth.add(jbtnReset);
    jpnlSouth.add(jbtnExit);

    /* Instantiate JButtons, put into a method for efficiency
    jbtn1 = instantiateJButton("1", Color.PINK);
    jbtn2 = instantiateJButton("2", Color.PINK);
    jbtn3 = instantiateJButton("3", Color.PINK);
    jbtn4 = instantiateJButton("4", Color.PINK);
    jbtn5 = instantiateJButton("5", Color.PINK);
    jbtn6 = instantiateJButton("6", Color.PINK);
    jbtn7 = instantiateJButton("7", Color.PINK);
    jbtn8 = instantiateJButton("8", Color.PINK);
    jbtn9 = instantiateJButton("9", Color.PINK);
    */

    //Finalize screen layout and publish to the display
    jpnlMain.add(jpnlCenter, BorderLayout.NORTH);
    jpnlMain.add(jpnlSouth, BorderLayout.CENTER);

    //Prepare the container
    Container ca = getContentPane();
    ca.setBackground (Color.LIGHT_GRAY);
    ca.add(jpnlMain);
    setContentPane (ca);

    setVisible(true);
    //end constructor       
}

//CLASS EVENT HANDLER
public void actionPerformed(java.awt.event.ActionEvent e)
{
    //find out which JButton was pressed by using the Action Command
    String sActionCommand = e.getActionCommand();

    //EXIT JButton
    if (sActionCommand == "Exit")
    {
        System.exit(0);
    }

    //RESET JButton
    else if (sActionCommand == "Reset")
    {
        jtfPlayerX.setText("");
        jtfPlayerO.setText("");
    }
}   //end ACTIONPERFORMED (java.awt.event.ActionEvent e)

/**
 * @param args
 */
public static void main(String[] args) {
    //EXECUTION STARTING POINT

    TicTacToeSwing TicTacToeSwing = new TicTacToeSwing();

    //TicTacToeSwing TicTacToeObject = new TicTacToeSwing();

}//end main(String[] args)

   }//end TicTacToeSwing class

推荐答案

stacktrace 包含有关导致 NPE 的原因的有用信息(jbtnReset 出现在第 84 行).因此你需要初始化jbtnReset:

The stacktrace has useful information about what is causing the NPE (jbtnReset which appears on line 84). Therefore you need to initialize jbtnReset:

JButton jbtnReset = new JButton("Reset");    

事实上,这同样适用于所有 jbtnTicTacToeX 按钮以及 jbtnExit.

In fact the same applys to all the jbtnTicTacToeX buttons as well as jbtnExit.

附带问题:使用 String#equals 比较 String 的内容.== 运算符比较 String 内容.

Side issues: Use String#equals to compare String content. The == operator compares String content.

if (sActionCommand == "Exit") {

应该

if (sActionCommand.equals("Exit")) {

这会检查带有此 String任何 组件的操作命令,因此您想要检查特定的源对象:

This checks the action command for any component with this String so you want to check the specific source object instead:

if (e.getSource() == jbtnExit) {

这篇关于Java - Tic Tac Toe Swing 游戏 - 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆