2013-04-22 127 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.