Java - Creating a Grid of Buttons Using a 2d Array -


this question has answer here:

as part of exercise, design program displays 9x9 grid of jbuttons. when click on button, button change in way, displays 'o', when click on it, displays 'x', or maybe changes colors, etc., while other buttons remain unchanged.

however, not sure how this. have created 2d array of jbuttons, , placed each on in 9x9 gridlayout panel. set actionlistener each one. problem is, not know how change color or text of 1 button. here short version of program displaying relevant parts.

private jbutton[][] t = new jbutton[9][9]; //declared earlier in program, right after class declaration.  public void buildtile() {     panelc.setlayout(new gridlayout(9, 9));     for(int r = 0; r < 9; r++)     {         for(int c = 0; c < 9; c++)         {             t[r][c] = new jbutton("o");             t[r][c].setbackground(color.black);             t[r][c].setforeground(color.white);             t[r][c].addactionlistener(new tilelistener());             panelc.add(t[r][c]);         }     } }  private class tilelistener implements actionlistener {     public void actionperformed(actionevent e)     {         //some code change specific button     } } 

how may specify button perform sort of aesthetic change to?

in actionlistener e.getsource() return button clicked


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -