如何让我的JButton更改Jlabel文本,以及我的JButton中的setText()?

时间:2015-08-06 02:02:12

标签: java swing nullpointerexception jbutton jlabel

我连续8个小时。我只是单独使用第一个按钮,试图操纵JLabel并在点击后在JButton上添加“X”。我选择了一个2D数组循环而不是硬编码每个按钮来保存代码行。为什么我一点击第一个(左上角)按钮就会得到一个空例外? 我的讲师坚持按钮数组是在构造函数内构建的,jlabel也必须在构造函数内构建。

发生错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TicTacToeGame.actionPerformed(TicTacToeGame.java:59)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

和我的班级:

> /**
 * Tic Tac Toe board game
 * 
 * @author Chris 
 * @version Challenge
 */
public class TicTacToeGame extends NscWindow implements java.awt.event.ActionListener {

    //declare fields       
    //private int turnCounter;
    private int CENTER;
    private javax.swing.JLabel label;
    private javax.swing.JButton [][] buttonList;    

    //build constructor
    public TicTacToeGame() {
    super(10, 10, 235, 280);
    this.setTitle("Tic-Tac-Toe");
    NscRectangle backColor = new NscRectangle(30, 30, 160, 160);
    backColor.setFilled(true);
    java.awt.Color newColor = new java.awt.Color(255, 0, 90);
    backColor.setBackground(newColor);
    this.add(backColor);
    this.repaint();    
    javax.swing.JButton[][] buttonList = new javax.swing.JButton[3][3];
    int x = 30;
    int y = 30;
    // nested loop to fill 2dimensional array with new buttons
    // add actionListener to each button, set size, set location
    // add to class, repaint    
    int i = 1;
    for (int row = 0; row < 3; row++) {
        for (int column = 0; column < 3; column++) {
            buttonList[row][column] = new javax.swing.JButton();
            buttonList[row][column].setSize(50, 50);
            buttonList[row][column].setLocation(x, y);
            buttonList[row][column].setActionCommand("button" + i);
            buttonList[row][column].addActionListener(this);            
            this.add(buttonList[row][column]);            
            x += 55;
            i++;
        }
        x = 30;
        y += 55;
    }  

    javax.swing.JLabel label = new javax.swing.JLabel("X's turn");
    label.setSize(160, 20);
    label.setLocation(30, 200);    
    label.setHorizontalAlignment(CENTER);    
    this.add(label);
    this.repaint();    
    }

    public void actionPerformed(java.awt.event.ActionEvent e) {        
        int turnCounter = 0;        
        if(e.getActionCommand().equals("button1") && turnCounter < 10) {            
            System.out.println("button1");
            label.setText("O's turn");            
            turnCounter++;
        } /** else if (source.equals("button2" )) {
            System.out.println("button2");
        } else if (source.equals("button3")) {
            System.out.println("button3");
        } else if (source.equals("button4")) {
            System.out.println("button4");
        } else if (source.equals("button5")) {
            System.out.println("button5");
        } else if (source.equals("button6")) {
            System.out.println("button6");
        } else if (source.equals("button7")) {
            System.out.println("button7");
        } else if (source.equals("button8")) {
            System.out.println("button8");
        } else if (source.equals("button9")) {
            System.out.println("button9"); 
        } */

    }   

    public static void main(String[] args) {
        new TicTacToeGame();        
    }
}

0 个答案:

没有答案