java - How can I set the dimension of some particular JButton component (in this case btnPlus)? -


i tried use setsize() didn't work. used setpreferredsize(new dimension(x,y)). yes work changes size of components in (opanel) panel. want change size of btnplus.

import javax.swing.*; import java.awt.event.*; import java.awt.*;  public class mycalc {  public static void main(string[] args) {      jframe main= new jframe();     jpanel bpanel= new jpanel();     jpanel opanel= new jpanel();     jpanel txt= new jpanel();     jpanel panel=new jpanel();     jbutton btn0= new jbutton("0");     jbutton btn1= new jbutton("1");     jbutton btn2= new jbutton("2");     jbutton btn3= new jbutton("3");     jbutton btn4= new jbutton("4");     jbutton btn5= new jbutton("5");     jbutton btn6= new jbutton("6");     jbutton btn7= new jbutton("7");     jbutton btn8= new jbutton("8");     jbutton btn9= new jbutton("9");     jbutton btndot= new jbutton(".");     jbutton btnplus= new jbutton("+");     jbutton btnminus= new jbutton("-");     jbutton btnequals= new jbutton("=");     //jtextfield tf= new jtextfield();     jtextarea ta= new jtextarea();      ta.setcolumns(20);     ta.setrows(3);     //ta.setviewportview()      main.setlayout(new borderlayout(20,20));      txt.setlayout(new borderlayout());     //txt.add(tf,borderlayout.north);     txt.add(ta,borderlayout.south);      bpanel.setlayout(new gridlayout(4,3,5,5));     bpanel.add(btn0);     bpanel.add(btn1);     bpanel.add(btn2);     bpanel.add(btn3);     bpanel.add(btn4);     bpanel.add(btn5);     bpanel.add(btn6);     bpanel.add(btn7);     bpanel.add(btn8);     bpanel.add(btn9);     bpanel.add(btndot);      opanel.setlayout(new gridlayout(3,1,5,5));     //opanel.setmaximumsize(new dimension(400,400));     opanel.add(btnplus);     opanel.add(btnminus);     opanel.add(btnequals);      btnplus.setpreferredsize(new dimension(50,100));     btnminus.setpreferredsize(new dimension(25,25));     btnplus.setpreferredsize(new dimension(50,100));      panel.setlayout(new flowlayout());     panel.add(bpanel);     panel.add(opanel);      //main.getcontentpane().add(bpanel,borderlayout.line_start);     //main.getcontentpane().add(opanel, borderlayout.line_end);     main.getcontentpane().add(panel,borderlayout.center);     main.getcontentpane().add(txt, borderlayout.page_start);      main.setsize(300, 300);     main.setdefaultcloseoperation(jframe.exit_on_close);     main.setvisible(true); }  } 

i think main problem in use of gridlayout, gives equal space components in container based on configuration give , size of container.

consider instead, using gridbaglayout, give more control.

see how use gridlayout , how use gridbaglayout more details.

you should take @ should avoid use of set(preferred|maximum|minimum)size methods in java swing? , yes, should avoiding it


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] -