swing - Java Tic Tac Toe GUI -
the code works fine problem want 3 columns , 3 rows.. output shows 6 columns instead of 3 rows , 3 columns
here code in problem appears.. here main class:
import java.awt.event.actionevent; import java.awt.event.actionlistener; import games.board.board; import games.board.cell; import games.board.mark; import javax.swing.jframe; import javax.swing.swingutilities; public class tictactoeguigame extends jframe { /** * @param args */ private board gb; private int turn; private void taketurn(cell c) { mark curmark = (turn++ % 2 == 0)? mark.nought : mark.cross; gb.setcell(curmark, c.getrow(), c.getcolumn()); } private tictactoeguigame() { gb = new board(3, 3, new actionlistener() { public void actionperformed(actionevent ae) { cell c = (cell) ae.getsource(); taketurn(c); } }); this.add(gb); this.setdefaultcloseoperation(exit_on_close); this.settitle("tic-tac-toe"); this.setsize(300, 300); this.setvisible(true); } public static void main(string[] args) { // todo auto-generated method stub swingutilities.invokelater( new runnable () { public void run() { new tictactoeguigame(); } }); } }
here board class:
package games.board; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jpanel; import javax.swing.jframe; public class board extends jpanel { private cell[][] cells; public board(int rows, int columns) { cells = new cell[rows][columns]; for( int r = 0; r < cells[0].length; r++ ) { (int c = 0; c < cells[1].length; c++) { cells[r][c] = new cell(r,c); } } } public board(int rows, int columns, actionlistener ah) { cells = new cell[rows][columns]; this.setlayout(new gridlayout()); for( int r = 0; r < cells.length; r++ ) { (int c = 0; c < cells[r].length; c++) { cells[r][c] = new cell(r,c); this.add(cells[r][c]); cells[r][c].addactionlistener(ah); } } } public void setcell(mark mark, int row, int column) throws illegalargumentexception { if (cells[row][column].getcontent() == mark.empty) cells[row][column].setcontent(mark); else throw new illegalargumentexception("player there!"); } public cell getcell(int row, int column) { return cells[row][column]; } public string tostring() { stringbuilder str = new stringbuilder(); for( int r = 0; r < cells.length; r++ ) { str.append("|"); (int c = 0; c < cells[r].length; c++) { switch(cells[r][c].getcontent()) { case nought: str.append("o"); break; case cross: str.append("x"); break; case yellow: str.append("y"); break; case red: str.append("r"); break; case blue: str.append("b"); break; case green: str.append("g"); break; case magenta: str.append("m"); break; case orange: str.append("m"); break; default: //empty str.append(""); } str.append("|"); } str.append("\n"); } return str.tostring(); } }
i want 3 columns , 3 rows.. output shows 6 columns
you need specify proper parameters gridlayout:
this.setlayout(new gridlayout(0, 3));
this tell grid contain 3 columns. number of rows depend on number of components add.
Comments
Post a Comment