swing - Labeling buttons and maintaining dimensions -


i struggling jbuttons, keeping them same size, seems adding button causes stretch accommodate said addition.

as result, button labeled 'yes' won't same size button labelled 'no', if while blank both buttons should have been more large enough comfortably contain either string.

essentially, know whether there way label buttons without altering dimensions of said buttons.

i employing gridbaglayout, here relevant sample of code reference:

edit

i managed effect looking using icons... won't able see in it's entirety, calling icons directly hard drive... regardless, i'll leave completed code reference...

game.java:

package com.sitrom.words.window;  import java.awt.canvas; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.io.ioexception;  public class game extends canvas implements runnable{      private static final long serialversionuid = 8433819262022712795l;      private boolean running = false;     private thread thread;      public synchronized void start(){          if(running){             return;         }          running = true;         thread = new thread(this);         thread.start();       }      public void run() {      }      public static void main(string[]args) throws ioexception{          new window(800,600,"words",new game());     }  } 

window.java:

package com.sitrom.words.window;  import java.io.ioexception; import java.io.inputstream; import java.io.pipedinputstream; import java.io.pipedoutputstream; import java.io.printstream; import java.io.printwriter;  import javax.swing.jframe; import javax.swing.jpanel;    public class window extends paneprimary{       public static class nav{             public static boolean a1;             public static boolean a_b1;             public static boolean a_c1;             public static boolean a_d1;             public static boolean a_e1;     }      public window(int w, int h, string title, game game) throws ioexception{          jframe frame = new jframe("words");           frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setlocationrelativeto(null);         frame.setlocation(0, 0);         frame.setextendedstate(jframe.maximized_both);         frame.setundecorated(true);            frame.add(game);         panecore((jpanel) frame.getcontentpane());         frame.pack();         game.start();         frame.setvisible(true);       }  } 

paneprimary.java:

package com.sitrom.words.window;  import java.awt.alphacomposite; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.font; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.textfield; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.pipedinputstream; import java.io.pipedoutputstream; import java.io.printstream; import java.io.printwriter; import java.util.list; import java.util.scanner; import javax.annotation.processing.messager; import javax.imageio.imageio; import javax.swing.borderfactory; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; //import javax.swing.jtextfield;       import com.sitrom.words.window.window.nav;  public class paneprimary {          public static void main(string[] args){            return;               }                    public static void panecore(jpanel panec){                  if(nav.a1 == true){                     system.out.println(".");                     panec.removeall();                     pane2(panec);                  }else{                     system.out.println("..");                     panec.removeall();                     pane1(panec);                 }             }              public static string repeat(int count, string with) {                 return new string(new char[count]).replace("\0", with);             }              public static string repeat(int count) {                 return repeat(count, " ");             }              public static void pane1(jpanel pane1) {                  jbutton button;                  final jtextarea texta = new jtextarea(11, 60);                 jscrollpane scroll;                  pane1.setbackground(color.black);                 pane1.setvisible(true);              pane1.setlayout(new gridbaglayout());             gridbagconstraints c = new gridbagconstraints();              scroll = new jscrollpane(texta);             c.fill = gridbagconstraints.both;             c.anchor = gridbagconstraints.page_start;             c.insets = new insets(10,10,0,10);             c.ipady = 300;             c.ipadx = 300;             c.weightx = 1;             c.weighty = 0.8;             c.gridwidth = 6;             c.gridx = 0;             c.gridy = 0;             texta.setfont(new font("8bitoperator jve", font.bold, 25));             texta.append(" segment a0... ");             texta.append((new string(new char[2]).replace("\0", "\n")));             texta.append(" ~~~~~~~~~");             texta.append((new string(new char[3]).replace("\0", "\n")));             texta.append(" begin?");             texta.append((new string(new char[3]).replace("\0", "\n")));             texta.append(" ~~~~~~~~~");             texta.append((new string(new char[2]).replace("\0", "\n")));             texta.append("* yes (y)");             texta.append((new string(new char[1]).replace("\0", "\n")));             texta.append("* no (n)");             texta.setbackground(color.black);             texta.setforeground(color.white);             scroll.setborder(borderfactory.createlineborder(color.white));               texta.setlinewrap(true);             texta.setwrapstyleword(false);             texta.seteditable(false);              pane1.add(scroll, c);              button = new jbutton();             c.fill = gridbagconstraints.both;             c.anchor = gridbagconstraints.below_baseline;             c.insets = new insets(15,10,10,10);             c.ipady = 40;             c.ipadx = 40;             c.weightx = 0.25;             c.weighty = 0.2;             c.gridwidth = 1;             c.gridx = 0;             c.gridy = 1;              button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_y.png"));             button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_y.png"));             button.setbackground(color.black);             button.setborder(borderfactory.createlineborder(color.white));             button.setfocuspainted(false);             button.addactionlistener(new actionlistener(){                  public void actionperformed(actionevent ae) {                     nav.a1 = true;                      pane1.setvisible(false);                      panecore(pane1);                  }});             pane1.add(button, c);              button = new jbutton();             c.fill = gridbagconstraints.both;             c.anchor = gridbagconstraints.below_baseline;             c.insets = new insets(15,10,10,10);             c.ipady = 40;             c.ipadx = 40;             c.weightx = 0.25;             c.weighty = 0.2;             c.gridwidth = 1;             c.gridx = 1;             c.gridy = 1;              button.setbackground(color.black);             button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_blank1.png"));             button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_blank1.png"));                         button.setborder(borderfactory.createlineborder(color.white));             button.setfocuspainted(false);              pane1.add(button, c);              button = new jbutton();             c.fill = gridbagconstraints.both;             c.anchor = gridbagconstraints.below_baseline;             c.insets = new insets(15,10,10,10);             c.ipady = 40;             c.ipadx = 40;             c.weightx = 0.25;             c.weighty = 0.2;             c.gridwidth = 1;             c.gridx = 2;             c.gridy = 1;              button.setbackground(color.black);             button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_blank1.png"));             button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_blank1.png"));             button.setborder(borderfactory.createlineborder(color.white));             button.setfocuspainted(false);              pane1.add(button, c);              button = new jbutton();             c.fill = gridbagconstraints.both;             c.anchor = gridbagconstraints.below_baseline;             c.insets = new insets(15,10,10,10);             c.ipady = 40;             c.ipadx = 40;             c.weightx = 0.25;             c.weighty = 0.2;             c.gridwidth = 1;             c.gridx = 3;             c.gridy = 1;              //button.setfont(new font("8bitoperator jve", font.bold, 25));             button.setbackground(color.black);             //button.setforeground(color.white);             button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_n.png"));             button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_n.png"));             //button.setmargin(null);             //button.setcontentareafilled(false);             button.setborder(borderfactory.createlineborder(color.white));             button.setfocuspainted(false);             button.addactionlistener(new actionlistener(){                  public void actionperformed(actionevent ae) {                     system.exit(0);                  }});             pane1.add(button, c);              }              public static void pane2(jpanel pane2){                  pane2.setbackground(color.black);                 pane2.setvisible(true);                  jscrollpane scroll;                              final jtextarea texta = new jtextarea(11, 30);                 jbutton button;                  pane2.setlayout(new gridbaglayout());                 gridbagconstraints c = new gridbagconstraints();                  scroll = new jscrollpane(texta);                 c.fill = gridbagconstraints.both;                 c.anchor = gridbagconstraints.page_start;                 c.insets = new insets(10,10,0,10);                 c.ipady = 300;                 c.ipadx = 300;                 c.weightx = 1;                 c.weighty = 0.8;                 c.gridwidth = 6;                 c.gridx = 0;                 c.gridy = 0;                 texta.append("segment a1..." + (new string(new char[5]).replace("\0", "\n") + "introductory exposition..." + (new string(new char[5]).replace("\0", "\n") + "you may travel north, south, east, or west...")));                 texta.setfont(new font("8bitoperator jve", font.bold, 25));                 texta.setbackground(color.black);                 texta.setforeground(color.white);                 scroll.setborder(borderfactory.createlineborder(color.white));                 texta.setlinewrap(true);                 texta.setwrapstyleword(false);                 texta.seteditable(false);                 pane2.add(scroll, c);                  button = new jbutton("");                 c.fill = gridbagconstraints.both;                 c.anchor = gridbagconstraints.below_baseline;                 c.insets = new insets(15,10,10,10);                 c.ipady = 40;                 c.ipadx = 40;                 c.weightx = 0.25;                 c.weighty = 0.2;                 c.gridwidth = 1;                 c.gridx = 0;                 c.gridy = 1;                  button.setbackground(color.black);                 button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_n.png"));                 button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_n.png"));                 button.setfocuspainted(false);                 button.setborder(borderfactory.createlineborder(color.white));                 button.addactionlistener(new actionlistener(){                      public void actionperformed(actionevent ae) {                         nav.a1 = false;                         nav.a_b1 = true;                          pane2.setvisible(false);                          panecore(pane2);                      }});                 pane2.add(button, c);                  button = new jbutton("");                 c.fill = gridbagconstraints.both;                 c.anchor = gridbagconstraints.below_baseline;                 c.insets = new insets(15,10,10,10);                 c.ipady = 40;                 c.ipadx = 40;                 c.weightx = 0.25;                 c.weighty = 0.2;                 c.gridwidth = 1;                 c.gridx = 1;                 c.gridy = 1;                 button.setbackground(color.black);                 button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_s.png"));                 button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_s.png"));                 button.setfocuspainted(false);                 button.setborder(borderfactory.createlineborder(color.white));                 button.addactionlistener(new actionlistener(){                      public void actionperformed(actionevent ae) {                         nav.a1 = false;                         nav.a_c1 = true;                          pane2.setvisible(false);                          panecore(pane2);                      }});                 pane2.add(button, c);                  button = new jbutton("");                 c.fill = gridbagconstraints.both;                 c.anchor = gridbagconstraints.below_baseline;                 c.insets = new insets(15,10,10,10);                 c.ipady = 40;                 c.ipadx = 40;                 c.weightx = 0.25;                 c.weighty = 0.2;                 c.gridwidth = 1;                 c.gridx = 2;                 c.gridy = 1;                 button.setbackground(color.black);                 button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_e.png"));                 button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_e.png"));                 button.setfocuspainted(false);                 button.setborder(borderfactory.createlineborder(color.white));                 button.addactionlistener(new actionlistener(){                      public void actionperformed(actionevent ae) {                         nav.a1 = false;                         nav.a_d1 = true;                          pane2.setvisible(false);                          panecore(pane2);                      }});                 pane2.add(button, c);                  button = new jbutton("");                 c.fill = gridbagconstraints.both;                 c.anchor = gridbagconstraints.below_baseline;                 c.insets = new insets(15,10,10,10);                 c.ipady = 40;                 c.ipadx = 40;                 c.weightx = 0.25;                 c.weighty = 0.2;                 c.gridwidth = 1;                 c.gridx = 3;                 c.gridy = 1;                 button.setbackground(color.black);                 button.seticon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\icon_w.png"));                 button.setpressedicon(new imageicon("c:\\users\\exmortis\\pictures\\projects\\pressedicon_w.png"));                 button.setfocuspainted(false);                 button.setborder(borderfactory.createlineborder(color.white));                 button.addactionlistener(new actionlistener(){                      public void actionperformed(actionevent ae) {                         nav.a1 = false;                         nav.a_e1 = true;                          pane2.setvisible(false);                          panecore(pane2);                      }});                 pane2.add(button, c);              }  } 

can add more code , perhaps screenshot show application looks (see stackoverflow.com/help/mcve more details on ideal code example on stack overflow like)?

i prefer use sub panels simple layout managers on using single panel large number of components , complex layout manager. consider using multiple panels and/or different layout managers (this nice overview of of layout managers available default: a visual guide layout managers.)

as short example, piece of code creates panel 2 buttons using gridlayout (with gaps , border add spacing):

import java.awt.*; import javax.swing.*; import javax.swing.border.*;  public class buttonwidths {     public static void main(final string[] arguments) {         swingutilities.invokelater(() -> new buttonwidths().createandshowgui());     }      private void createandshowgui() {         final jframe frame = new jframe("stack overflow");         frame.setbounds(100, 100, 240, 200);         frame.setdefaultcloseoperation(windowconstants.exit_on_close);          final jpanel panel = new jpanel(new gridlayout(2, 1, 28, 28));         panel.setborder(new emptyborder(28, 28, 28, 28));         final jbutton yesbutton = new jbutton("yes");         final jbutton nobutton = new jbutton("no");         panel.add(yesbutton);         panel.add(nobutton);          frame.getcontentpane().add(panel);         frame.setvisible(true);          system.out.println("yesbutton: " + yesbutton.getsize());         system.out.println("nobutton: " + nobutton.getsize());     } } 

setting minimum size?

the gridbaglayout should use button's minimum (and/or preferred) sizes, might way make width of buttons equal; gridbaglayout documentation:

in addition constraints object, gridbaglayout considers each component's minimum , preferred sizes in order determine component's size.

however, search returned several stack overflow discussions using minimum size advised against:


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -