java - Resizing JButton causes it to disappear -


i want of buttons same size. 1 text "open save" widest, want use 1 metric resizing other buttons. however, when have line of code in program, button won't display: newbutton.setpreferredsize (opensavebutton.getsize ());

here's full program far:

public class project1 implements actionlistener {      // frames     private jframe mainmenu = new jframe("main menu");     private jpanel mainpanel = new jpanel();     private jframe game = new jframe("game");     private jpanel gamepanel = new jpanel();     // controls     private jbutton newbutton = new jbutton("new");     private jbutton opensavebutton = new jbutton("open save");     private jbutton exitbutton = new jbutton("exit");     private jtextarea textarea = new jtextarea();      public static void main(string[] args) {         new project1();     }  // constructors     public project1() {          mainpanel.setlayout(new gridbaglayout());         additem(mainpanel, newbutton, 0, 0, 1, 1, gridbagconstraints.center);         newbutton.addactionlistener(this);         newbutton.setpreferredsize(opensavebutton.getsize());         additem(mainpanel, opensavebutton, 0, 1, 1, 1, gridbagconstraints.center);         opensavebutton.addactionlistener(this);         additem(mainpanel, exitbutton, 0, 2, 1, 1, gridbagconstraints.center);         exitbutton.addactionlistener(this);          mainmenu.add(mainpanel, borderlayout.north);         openframe(mainmenu, 10, 10, jframe.exit_on_close);          gamepanel.setlayout(new gridbaglayout());         additem(gamepanel, textarea, 0, 0, 1, 1, gridbagconstraints.center);         openframe(game, 200, 10, jframe.do_nothing_on_close);      }  // setters // getters // actionlistener override     public void actionperformed(actionevent e) {          if (e.getsource() == newbutton) {             newgame();         } else if (e.getsource() == opensavebutton) {             openfile();         } else if (e.getsource() == exitbutton) {             system.exit(0);         }     }  // other functions , procedures     private void openframe(jframe what, int x, int y, int operation) {          what.pack();         what.setlocation(x, y);         what.setvisible(true);         what.setresizable(false);         what.setdefaultcloseoperation(operation);     }      private void additem(jpanel p, jcomponent c, int x, int y, int width, int height,                     int align) {          gridbagconstraints gc = new gridbagconstraints();         gc.gridx = x;         gc.gridy = y;         gc.gridwidth = width;         gc.gridheight = height;         gc.weightx = 100.0;         gc.weighty = 100.0;         gc.insets = new insets(5, 5, 5, 5);         gc.anchor = align;         gc.fill = gridbagconstraints.none;         p.add(c, gc);     }      private void newgame() {      }      private void openfile() {      }  } 

here 2 screen shots of happens, without line , with:

without resizingwith resizing

however, when have line of code in program, button won't display: newbutton.setpreferredsize (opensavebutton.getsize ());

then, don't this.

when call opensavebutton.getsize(), opensavebutton has no size, it's 0x0, you're telling layout manager newbutton should have preferred size of 0x0, it's kindly applying , button no longer visible.

now, if surmise correctly you're trying do, try chaning gc.fill = gridbagconstraints.none; gc.fill = gridbagconstraints.horizontal; in additem method.

see how use gridbaglayout more details

you may want take @ should avoid use of set(preferred|maximum|minimum)size methods in java swing?, the use of multiple jframes, good/bad practice? , initial threads


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -