Java Swing - JPanel and GridLayout Margins/Padding -
i'm working on building chess game in java, , i'm having bit of trouble getting gui way want swing. i'm using gridlayout
organize grid of 8x8 chessbutton
s (which override jbutton
can store information inside of them such coordinates). originally, chessbutton
s wouldn't appear unless moused on them, solved problem placing each chessbutton
inside separate jpanel
, setting each button's setpreferredsize()
set height , width.
now, problem there seems small margin or padding above (and/or below?) each button. i've made sure set sethgap(0)
, setvgap(0)
gridlayout
, i'm pretty sure mysterious margin coming either buttons or jpanel
s. but, can't seem rid of them, , seem causing each chessbutton
shift little bit up/down whenever mouse of them.
i realize description of problem might little hard visualize, i've taken screenshot (using jbutton
s rather chessbutton
s gaps easier recognize): http://img3.imageshack.us/img3/6656/jbuttonmargins.png
here code used initialize each chessbutton
:
chessboard = new jpanel(new gridlayout(8, 8, 0, 0)); chessboard.setborder(borderfactory.createemptyborder()); (int = 0; <= 65; i++) { //create new chessbutton chessbutton button = new chessbutton("hi"); button.setborder(borderfactory.createemptyborder()); button.setpreferredsize(new dimension(75, 75)); button.setmargin(new insets(0, 0, 0, 0)); //create new jpanel chessbutton go jpanel buttonpanel = new jpanel(); buttonpanel.setpreferredsize(new dimension(75, 75)); buttonpanel.setborder(borderfactory.createemptyborder()); buttonpanel.add(button); //add buttonpanel grid chessboard.add(buttonpanel); }
so, how can rid of these vertical spaces between buttons? i'm relatively new swing, i'm sorry if answer extremely obvious, i'd appreciate might have offer! in advance!
originally, chessbuttons wouldn't appear unless moused on them, solved problem placing each chessbutton inside separate jpanel , setting each button's setpreferredsize() set height , width
that not proper solution. there no reason use jpanel hold buttons. in fact, cause of problem. buttons should show when add them gridlayout. if don't show because added buttons gui after making gui visible. components should added gui before made visible.
now, problem there seems small margin or padding above (and/or below?) each button
i don't understand why there isn't horizontal gap. when create jpanel, default uses flowlayout contains horizontal/vertical gap of 5 pixels. understand why might have vertical gap of 10 pixels. don't understand why there no horizontal gap.
if need more post sscce demonstrating problem. , sscce should use regular jbuttons. basics working standard components before start playing custom components. way know if problem custom code or not.
Comments
Post a Comment