Question

I have 9 scenarios making a 9 conditional statement that will look to see if all the my JButton have been pressed and no winner was found, but it is giving me " error: ' void ' type not allowed here.

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

public class TicTacToe extends JFrame
{
private final int HEIGHT = 450;
private final int WIDTH = 500;
private static JButton [] button = new JButton[9];
private static Action [] playerTurn = new Action[9];
private static JLabel [] label;
private int player = 1;
private Font arial = new Font("Arial", Font.BOLD, 20);
private static int lockButtons = 0;


public TicTacToe ()
{
    setTitle( " tic tak tow ");
    setSize( HEIGHT, WIDTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    setLayout(new GridLayout(4,3));

    int num = 0;
    for(int i = 0; i < 9; i++ )
    {

        button[i] = new JButton( "B" + i + 1);
        playerTurn[i] = new Action();
        add(button[i]);
        button[i].setFont(arial);
        button[i].addActionListener(playerTurn[i]);
    }


    setVisible(true);
}

private class Action implements ActionListener
{
    public void actionPerformed(ActionEvent playerMove)
    {
        //Get button pressed using GetSource Command
        JButton whatPlayer=(JButton)(playerMove.getSource());

            if(player == 1)
            {
                player++;
                whatPlayer.setText("player1");
                whatPlayer.setEnabled(false); 
                validate(); 
                return;
            }               
            if (player == 2)
            {
                player--;
                whatPlayer.setText("player2");
                whatPlayer.setEnabled(false);
                validate(); 
                return;
            }
    }
    public void validate()
    {
        if(button[0].getText().equals(button[1].getText()) && button[1].getText().equals(button[2].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
        }
        if(button[3].getText().equals(button[4].getText()) && button[4].getText().equals(button[5].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[3].getText());
            gameOver();
        }
        if(button[6].getText().equals(button[7].getText()) && button[7].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[6].getText());
            gameOver();
        }
        if(button[0].getText().equals(button[3].getText()) && button[3].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
        }
        if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
            gameOver();
        }
        if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
            gameOver();
        }
        if(button[2].getText().equals(button[5].getText()) && button[5].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
        }
        if(button[0].getText().equals(button[4].getText()) && button[4].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
        }
        if(button[2].getText().equals(button[4].getText()) && button[4].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
        }
        if ( button.length == setEnabled(false))  // this is the problem.  Don't know if I worded it correctly
        {
            JOptionPane.showMessageDialog(null,"This was a Draw");
        }

    }
    public void gameOver()
    {
        for( int x = 0; x < button.length; x++)
        {
            button[x].setEnabled(false);
        }
    }


}
public static void main(String[] arg)
{
    new TicTacToe();
}   
}
Was it helpful?

Solution

button.length will be equal to 9 since you defined an array of 9 elements

private static JButton [] button = new JButton[9];

How can you do this if ( button.length == setEnabled(false)) comparision in if loop, setEnabled(false) is going to disable the JFrame and the return type is void,here you are comparing the int with void that's why the error is thrown at the compile time.

int i;

for(i=0;i<button.length;i++){
     if(b[i].isEnabled()){
     break;
     }
}

if(i==button.length){
JOptionPane.showMessageDialog(null,"This was a Draw");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top