java - Receiving " error: ' void ' type not allowed when checking JButton status in Tic Tac Toe game -
i have 9 scenarios making 9 conditional statement see if jbutton have been pressed , no winner found, 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 = 0; < 9; i++ ) { button[i] = new jbutton( "b" + + 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 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 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 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 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 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 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 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 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 winner is" + button[2].gettext()); gameover(); } if ( button.length == setenabled(false)) // problem. don't know if worded correctly { joptionpane.showmessagedialog(null,"this draw"); } } public void gameover() { for( int x = 0; x < button.length; x++) { button[x].setenabled(false); } } } public static void main(string[] arg) { new tictactoe(); } }
button.length equal 9 since defined array of 9 elements
private static jbutton [] button = new jbutton[9];
how can if ( button.length == setenabled(false))
comparision in if loop, setenabled(false)
going disable jframe , return type void,here comparing int void that's why error thrown @ compile time.
int i; for(i=0;i<button.length;i++){ if(b[i].isenabled()){ break; } } if(i==button.length){ joptionpane.showmessagedialog(null,"this draw"); }
Comments
Post a Comment