java - JComponents not drawn correctly -


i trying add components jpanel, aren't sized correctly , not in right location. code componenent.

button = new jbutton(); button.setsize(100, 100); button.setlocation(400, 400); button.setcursor(new cursor(cursor.crosshair_cursor)); vm.panel.add(button); 

it's behind other things i'm drawing, it's not visible (that's why made cursor crosshair, see button was). i'm drawing.

g.drawimage(imageio.read(getclass().getresourceasstream("/background.jpg")), 0, 0, vm.panel.getwidth(), vm.panel.getheight(), null); g.setcolor(color.white); g.fillroundrect(200, 200, 880, 560, 100, 100); g.setcolor(color.black); g.setfont(new font("arial", font.plain, 48)); g.drawstring("login", 575, 300); 

however size , location button aren't set correctly.

the default layout manager jpanel flowlayout. layout manager determine size , location of button.

so use appropriate layout manager button displayed how want it.

for example want button displayed in center of panel use gridbaglayout.

panel.setlayout( new gridbaglayout() ); panel.add(button, new gridbagconstraints()); 

read swing tutorial on layout managers more information , examples.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -