2013-04-22 130 views
0

我正在制作一个tic tac脚趾游戏,并且制作了gui并想运行它来测试它,但是出现了一些错误。我不知道为什么,并希望有人能够解释为什么这些错误即将出现,以及我应该如何解决这些错误。是我得到的错误如下所示:异常线程 “main” 显示java.lang.NullPointerException 在TicTacToeSwing(TicTacToeSwing.java:84) 在TicTacToeSwing.main(TicTacToeSwing.java:180)Java - 井字游戏Swing游戏 - 错误

。这里是我的代码:(请注意:我还没有完成计算,因为我想让gui先走)如果您知道我应该考虑采取更好的做法,请务必让我知道。

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 
+0

什么行是被抛出的异常? – 2013-04-22 15:55:36

回答

4

堆栈跟踪具有大约什么原因造成的NPEjbtnReset出现在线路84)的有用信息。因此,你需要初始化jbtnReset

JButton jbtnReset = new JButton("Reset");  

其实同applys所有jbtnTicTacToeX按钮以及jbtnExit


方的问题: 使用String#equals比较String内容。 ==运营商比较String内容。

if (sActionCommand == "Exit") { 

应该

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

此检查任何组件与此String,所以你要检查的具体来源对象,而不是动作命令:

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

第二方问题句子:s/content/references/ – Crisfole 2013-04-22 16:24:08

+0

为什么downvote? – Reimeus 2013-04-22 16:28:27

+0

是不是我的..... – Crisfole 2013-04-22 16:30:23

1

如果你有:

private JButton jbtnExit; 
private JButton jbtnReset; 

尝试:

private JButton jbtnExit = new JButton("Exit"); 
private JButton jbtnReset = new JButton("Reset"); 
+0

我没有downvote它......我想知道为什么有人会自己这样做。感谢大家的帮助和投入。除了这一项上线24我已经清除了所有的错误:\t \t公共类TicTacToeSwing扩展JFrame的 实现的ActionListener {\t \t在该行 多个标记 - 可序列化类TicTacToeSwing没有声明long类型的静态最后的serialVersionUID领域 - 发生'TicTacToeSwing' – tooheymomster 2013-04-22 17:13:10

1

如果你看一下堆栈跟踪,像TicTacToeSwing.java:84地告诉你在哪里的误差存在的。 TicTacToeSwing.java是文件,而:84表示行号84.