java - Adding buttons to a JDialog? -


i'm trying make jbutton on jdialog, but, button cover entire jdialog, on this? looks like:

enter image description here

this how create jdialog , jbutton:

class menustorehandler implements actionlistener{     public void actionperformed(actionevent e){          dimension dim = toolkit.getdefaulttoolkit().getscreensize();         int screenwidth = (int) dim.getwidth();         int screenheight = (int) dim.getheight();          jdialog g = new jdialog();         g.settitle("the store");         g.setsize(200, 200);         g.setlocation(screenwidth / 2 - 150, screenheight / 2 - 150);          jbutton b = new jbutton("buy");         b.addactionlistener( new storeitem1handler() );         b.setvisible(true);         g.add(b);          g.setvisible(true);     } } 

i'm going post full mrstan.class, here is:

package progress;  public class mrstan extends jpanel{      private timer timer = new timer();     public static int points;     static file h = new file("text.txt");     public imageicon bg = new imageicon("d:/mrstan/bg.png");     static jmenubar menubar;     formatter x;     jmenu menu;     jmenuitem menuitem;      double version = 0.3;      class todotask extends timertask{         public void run(){              points += 1;             repaint();         }     }      public int getpoints(){         return points;     }      public void setpoints( int points ){         this.points = points;     }      public mrstan(){         setignorerepaint(true);          menubar = new jmenubar();         menu = new jmenu("menu");         menu.setmnemonic(keyevent.vk_f);         menu.getaccessiblecontext().setaccessibledescription("menu");         menubar.add(menu);          menuitem = new jmenuitem("store (s)", new imageicon("coins.png"));         menuitem.setmnemonic(keyevent.vk_s);         menuitem.addactionlistener( new menustorehandler() );         menu.add(menuitem);          menuitem = new jmenuitem("reset points (r)", new imageicon("delete.png"));         menuitem.setmnemonic(keyevent.vk_r);         menuitem.addactionlistener( new menuresetpointhandler() );         menu.add(menuitem);          // add separator         menu.addseparator();          menuitem = new jmenuitem("exit (e)", new imageicon("cross.png"));         menuitem.setmnemonic(keyevent.vk_e);         menuitem.addactionlistener( new menuexithandler() );         menu.add(menuitem);          timer.schedule(new todotask(), 0, 2000);      }      class menustorehandler implements actionlistener{         public void actionperformed(actionevent e){              dimension dim = toolkit.getdefaulttoolkit().getscreensize();             int screenwidth = (int) dim.getwidth();             int screenheight = (int) dim.getheight();              jdialog g = new jdialog();             g.settitle("the store");             g.setsize(200, 200);             g.setlocation(screenwidth / 2 - 150, screenheight / 2 - 150);              jbutton b = new jbutton("buy");             b.addactionlistener( new storeitem1handler() );             b.setvisible(true);             g.add(b);              g.setvisible(true);         }     }      class storeitem1handler implements actionlistener{         public void actionperformed(actionevent e){             system.out.println("store-button 1 pressed.");         }     }      class menuexithandler implements actionlistener{         public void actionperformed(actionevent e){             system.exit(1);         }     }      class menuresetpointhandler implements actionlistener{         public void actionperformed(actionevent e){             points = 0;             repaint();             joptionpane.showmessagedialog(null, "points have been reset.");         }     }      public void paint(graphics g){         g.setcolor(color.white);         bg.painticon(this,g,0,0);         g.setcolor(color.black);         g.drawstring("points: " + points, 75, 95);         g.drawstring("version: " + version, 2, 10);     }      public static void main(string[] args){          final mrstancreatefile g = new mrstancreatefile();          runtime.getruntime().addshutdownhook(new thread(new runnable(){             public void run(){                 if(h.exists()){                     g.openfile();                     g.addrecords();                     g.closefile();                 }else{                     system.out.println(h.getname() + "does not exist, not saving.");                 }             }         }, "shutdown-thread"));          readit();          //create new jframe         jframe frame = new jframe();         frame.settitle("mrstan");         frame.setsize(200, 200);         frame.setdefaultcloseoperation( jframe.exit_on_close );         frame.setjmenubar(menubar);          //set location of jframe         dimension dim = toolkit.getdefaulttoolkit().getscreensize();         int screenwidth = (int) dim.getwidth();         int screenheight = (int) dim.getheight();         frame.setlocation(screenwidth / 2 - 200, screenheight / 2 - 200);          //set contentpane jpanel         mrstan panel = new mrstan();         frame.setcontentpane(panel);          //make user not able resize         frame.setresizable(false);          //make jframe visible         frame.setvisible(true);     }      public static void readit(){         mrstanreadfile r = new mrstanreadfile();         r.openfile();         r.readfile();         r.closefile();     }  } 

why covering entire jdialog? i'm using basic layout manager, should fine.

try adding button contentpane first , setting bounds later.

container pane = dialog.getcontentpane(); pane.setlayout(null); jbutton button = new jbutton("testbutton!"); pane.add(button); button.setbounds(10,10,40,40); 

Comments