Text not showing on my button at runtime

I am trying to code tic-tac-toe. I want the button to change to "X" when I click on it.

I'm not quite with my code.
But I want to see the button change text. But I don't see any changes.
SetText (); the method suits me. :)

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

public class tictac extends JFrame implements ActionListener{

    JButton [][] game = new JButton[3][3];
    JLabel head = new JLabel("Tic-Tac-Toe");
    Font font = new Font("Time New Roman", Font.ITALIC, 20);
    GridLayout grid = new GridLayout(3,3,0,0);
    int row,column =0;
    FlowLayout flow = new FlowLayout();

    tictac(){
        //setLayout(flow);
        //add(head);
        super("Tic-Tac-Toe");
        setLayout(grid);
        setVisible(true);
        setDefaultCloseOperation(1);
        setSize(500,500);

        for (row =0; row<3;row++ ){
            for(column =0; column <3;column++){
                game[row][column]= new JButton("");
                add(game[row][column]);
                game[row][column].addActionListener(this);
            }
        }
    }


    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();

        if (source == game[row][column])
            game[row][column].setText("X");
        System.out.println("X");
    }   
}

      

What am I doing wrong?

+3


source to share


1 answer


You are using row

and incorrectly column

.
These values ​​were updated (both are now 3) in your double loop when you created these JButtons.
When you access them again actionPerformed

, one problem is that it throws an ArrayIndexOutOfBoundsException.

Note. It's just a quick fix.
Declare # of rows (ROWS) and # of columns (COLS) as constants.
When the action is running, test each of these buttons with a double for loop.

Code:



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

public class QuickTester {

    public static void main (String [] args) {
        TicTac tt = new TicTac();
    }
}

class TicTac extends JFrame implements ActionListener{

    JButton [][] game = new JButton[3][3];
    JLabel head = new JLabel("Tic-Tac-Toe");
    Font font = new Font("Time New Roman", Font.ITALIC, 20);
    GridLayout grid = new GridLayout(3,3,0,0);
    FlowLayout flow = new FlowLayout();

    static final int ROWS = 3;
    static final int COLS = 3;

    TicTac(){
        //setLayout(flow);
        //add(head);
        super("Tic-Tac-Toe");
        setLayout(grid);
        setVisible(true);
        setDefaultCloseOperation(1);
        setSize(500,500);

        for (int row = 0; row < ROWS; row++ ){
            for(int col = 0; col < COLS; col++){
                game[row][col]= new JButton("");
                add(game[row][col]);
                game[row][col].addActionListener(this);
            }
        }
    }


    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();

        for(int row = 0; row < ROWS; row++) {
            for(int col = 0; col < COLS; col++) {

                if (source == game[row][col])
                    game[row][col].setText("X");
                System.out.println("X");
            }
        }
    }
}

      

Output:

TicTacToe

+3


source







All Articles