java - JButton not properly shown in JFrame -


this question has answer here:

i making jframe menu has 3 buttons buttons aren't shown correctly when started

here's code:

import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel;  public class mainmenu {     public static void main(string[] args) {         jframe frame = new jframe("nakib group managment system");         frame.setsize(500, 200);         frame.setvisible(true);         frame.setdefaultcloseoperation(jframe.exit_on_close);         jpanel panel = new jpanel();         jbutton addrequest = new jbutton("add request");         addrequest.addactionlistener(new addrequest());         jbutton viewrequests = new jbutton("view requests");         viewrequests.addactionlistener(new viewrequests());         jbutton addcab = new jbutton("add cab");         addcab.addactionlistener(new addcab());         panel.add(addrequest);         panel.add(viewrequests);         panel.add(addcab);         frame.add(panel);     } } 

when run show me following (can't post image because not enough reputation): first run

however, when resize window buttons show: resized

my os environment windows 10 , i'm working on java.

the problem setting frame visible before adding components it, causes component hierarchy invalid. the docs add method:

if container has been displayed, hierarchy must validated thereafter in order display added component.

to correct this, should move line frame.setvisible(true) end, after add panel. or, alternatively, can call revalidate , repaint @ end, force updating , repainting.

right works after resizing because forces lay out again, , correctly updates component hierarchy.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -