java - Action event on a JButton matrix -
java - Action event on a JButton matrix -
i can't handle event in matrix of jbuttons. need figure out button pressed, alter objects color match button.
i using code:
private class matrixbuttonlistener implements actionlistener { public void actionperformed(actionevent e) { jbutton btn = (jbutton) (e.getsource()); (int = 0; < matrixbouton.length; i++) { (int j = 0; j < matrixbouton[i].length; j++) { btn.equals(matrixbouton[i][j]); if (btn.getbackground() == color_neutral) { btn.setbackground(color_player); } } } } }
instead of looping through jbuttons
trace button using evt.getsource()
. homecoming reference actual button pressed. , can perform wish. can utilize next simplified code indeed:
private class matrixbuttonlistener implements actionlistener { public void actionperformed(actionevent e) { jbutton btn = (jbutton) (e.getsource()); if (btn.getbackground() == color_neutral) { btn.setbackground(color_player); } } }
java swing jbutton actionlistener
Comments
Post a Comment