i trying design ui tic tac toe in java don't understand why 5th row in ui.i want have 4 rows bottom 3 rows serve buttons board of tictactoe , top row used display toshow important messages users . have attached code , snapshot of ui.
//code package tictactoe; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class tictactoeui extends jframe implements actionlistener { private static final long serialversionuid = 1l; jpanel[] row = new jpanel[4]; jbutton[] button = new jbutton[9]; string[] buttonstring = {" "," "," ", " "," "," ", " "," "," "}; dimension displaydimension = new dimension(290, 45); dimension regulardimension = new dimension(90, 90); jtextarea display = new jtextarea(2, 20); font font = new font("times new roman", font.bold, 14); public tictactoeui(){ super("tictactoe"); setdesign(); setsize(350, 500); setresizable(false); setdefaultcloseoperation(exit_on_close); gridlayout grid = new gridlayout(5,5); setlayout(grid); flowlayout f1 = new flowlayout(flowlayout.center); flowlayout f2 = new flowlayout(flowlayout.center,1,1);//gap in cells for(int = 0; < 4; i++ ) row[i] = new jpanel(); row[0].setlayout(f1); for(int = 1; < 4; i++) row[i].setlayout(f2); for(int = 0; < 9; i++) { button[i] = new jbutton(); button[i].settext(buttonstring[i]); button[i].setbackground(color.black); button[i].setfont(font); button[i].addactionlistener(this); } display.setfont(font); display.seteditable(false); display.setcomponentorientation(componentorientation.right_to_left); display.setpreferredsize(displaydimension); for(int = 0; < 9; i++) button[i].setpreferredsize(regulardimension); row[0].add(display); add(row[0]); for(int i=1,k=0;i<4&&k<9;i++){ for(int j=0;j<3;j++){ row[i].add(button[k]); k++; } add(row[i]); } setvisible(true); } public final void setdesign() { try { uimanager.setlookandfeel( "com.sun.java.swing.plaf.nimbus.nimbuslookandfeel"); } catch(exception e) { } } public static void main(string[] args) { tictactoeui obj = new tictactoeui(); } @override public void actionperformed(actionevent ae) { // todo auto-generated method stub int x,y; if(ae.getsource()==button[0]){ x=0; y=0; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[1]){ x=0; y=1; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[2]){ x=0; y=2; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[3]){ x=1; y=0; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[4]){ x=1; y=1; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[5]){ x=1; y=2; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[6]){ x=2; y=0; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[7]){ x=2; y=1; display.settext("x = " + x + "y = " + y); } else if(ae.getsource()==button[8]){ x=2; y=2; display.settext("x = " + x + "y = " + y); } } } 
well, i'm not sure if understand question, because problem have in line:
gridlayout grid = new gridlayout(5,5); i want have 4 rows change to:
gridlayout grid = new gridlayout(4,5); hope helps.
Comments
Post a Comment