java - How to place a JButton at a specific x - y point on a JPanel -
general code: issues below reference classes
public class board extends jpanel { public board() { boardmethods = new boardmethods();//boardmethods handles logic a.printboard(getboard());//irrelevant question, helps determine winner jpanel panellordy = new jpanel(new flowlayout()); //jbutton alphabutton = new jbutton("hi"); //alphabutton.setbounds(213,131,100,100); //add(alphabutton); } public int[][] getboard(){ return boardevalmatrix; } public void paintcomponent(graphics g){ g.setcolor(color.blue); g.fillrect(0, 0, 1456, 916); graphics2d newg = (graphics2d) g; newg.setstroke(new basicstroke(15)); g.setcolor(color.yellow); for(int = 0; < 6; a++)//rows board --- rowheight 127 g.drawrect(128, 68 + (a*127), 1200, 127); g.setcolor(color.black); newg.setstroke(new basicstroke(8)); for(int = 0; < 6; a++)//columns board --- columnwidth 171 g.drawrect(128 + (a*171), 68, 171, 764); for(int = 0; < 6; a++)//give rows black line in middle g.drawrect(128, 68 + (a*127), 1200, 127); //g.drawstring("h", 213, 131); center point //g.drawline(50,0, 1456, 916); //width 1456 length 916 - school computer monitors jbutton alphabutton = new jbutton("hi"); alphabutton.setbounds(213,131,100,100); bored.add(alphabutton); } } public class gameboard extends jpanel { private board bored; private roundbutton[][] buttonarray;//holds roundbuttons on board private jpanel resetpanel, quitpanel; public gameboard() { setlayout(new borderlayout()); buttonarray = new roundbutton[6][7]; for(int = 0; < buttonarray.length; a++) for(int b = 0; b < buttonarray[0].length; b++) bored = new board(); add(bored, borderlayout.center); resetpanel = new jpanel(); resetpanel.setlayout(new flowlayout()); this.add(resetpanel, borderlayout.north); quitpanel = new jpanel(); quitpanel.setlayout(new flowlayout()); this.add(quitpanel, borderlayout.south); jbutton resetbutton = new jbutton("reset"); resetbutton.addactionlistener(new resetlistener()); resetpanel.add(resetbutton);//resets board jbutton quitbutton = new jbutton("quit"); quitbutton.addactionlistener(new quitlistener()); quitpanel.add(quitbutton); //jbutton alphabutton = new jbutton("hi"); //alphabutton.setbounds(213,131,100,100); //bored.add(alphabutton); } }
issue 1:
i found question regarding how place jbutton
@ location, however, have circular appearing jbutton
s , need jbutton
centered on point, in case, 213,131
. using setbounds(int x, int y, int width, int height)
method place jbutton
. there way have jbutton
center on coordinate mentioned above, or have play numbers want? line can found in both classes above, testing , found issue described in issue 2.
jbutton alphabutton = new jbutton("hi"); alphabutton.setbounds(213,131,100,100); add(alphabutton);
issue 2:
if @ class board
, in paintcomponent
method use setbounds()
method place jbutton
. works , places button believe correct dimensions on jpanel
when paintcomponent()
method called top left corner in correct location. however, when screen refreshed, paintcomponent()
again called , number of buttons continues increase, fun mess not want. however, when move setbounds
method , alphabutton
anywhere outside of board
's paintcomponent()
method, is, putting class board
painted on, inside board
constructor, jbutton
propagation issue ceases, however, jbutton
no longer places @ correct coordinate point , loses dimensions gave it. how fix this?
p.s. see setbounds
method several times. in real code, setbounds
method , jbutton
calling exist in 1 class @ time, entered here multiple times show placed it.
never use setbounds()
, stick layout manager.
flowlayout
will automatically center component. better use borderlayout
, , set center
.
Comments
Post a Comment