java - How do I create spacing in between JButtons with GridBagLayout? -


i trying create little main menu simple scheduling program right now, having bit of difficulty having space in between buttons. stick right next each other, there gap between each button.

i have tried use weightx, weighty commands nothing seems change. have blank space between edges of gui , sides of buttons, , between each button.

here's screenshot

enter image description here

import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*;  public class scheduler {  jbutton vday, vweek, task, exit; jframe wframe, dframe, tframe;  jlabel head;     public void createframe() {     jframe frame = new jframe("main menu");           buttonlistener btnlst = new buttonlistener();     jpanel panel = new jpanel();     panel.setlayout(new gridbaglayout());     gridbagconstraints c = new gridbagconstraints();      c.weightx = 1;     c.weighty = .25;     c.gridx = 0;     c.gridy = 0;     c.gridwidth = 1;     c.gridheight = 1;     c.fill = gridbagconstraints.both;     head = new jlabel("the plain scheduler");     panel.add(head, c);     c.weightx = 1;     c.weighty = .25;     c.gridx = 0;     c.gridy = 1;     c.gridheight = 2;     c.gridwidth = 3;     vday = new jbutton("view day");     panel.add(vday, c);     c.weightx = 1;     c.weighty = .25;     c.gridx = 0;     c.gridy = 3;     c.gridheight = 2;     c.gridwidth = 3;     vweek = new jbutton("view week");     panel.add(vweek,c);     c.weightx = 1;     c.weighty = .25;     c.gridx = 0;     c.gridy = 5;     c.gridheight = 2;     c.gridwidth = 3;     task = new jbutton("assign/edit tasks");     panel.add(task, c);     c.weightx = 1;     c.weighty = .25;     c.gridx = 0;     c.gridy = 7;     c.gridheight = 1;     c.gridwidth = 2;     exit = new jbutton("exit");     panel.add(exit, c);      vday.addactionlistener(btnlst);     vweek.addactionlistener(btnlst);     task.addactionlistener(btnlst);     exit.addactionlistener(btnlst);      frame.add(panel);     frame.pack();     frame.setvisible(true);     frame.setdefaultcloseoperation(jframe.exit_on_close);     }     public static void main(string[] args) {     scheduler scheduler = new scheduler();     scheduler.createframe();     } } 

you can use gridbagconstraints#insets define amount of spacing/padding added cell, example...

buttons

import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class scheduler {      jbutton vday, vweek, task, exit;     jframe wframe, dframe, tframe;      jlabel head;      public void createframe() {         jframe frame = new jframe("main menu");          jpanel panel = new jpanel();         panel.setlayout(new gridbaglayout());         gridbagconstraints c = new gridbagconstraints();          c.weightx = 1;         c.weighty = .25;         c.insets = new insets(5, 0, 5, 0);         c.gridwidth = gridbagconstraints.remainder;         c.fill = gridbagconstraints.both;         head = new jlabel("the plain scheduler");         panel.add(head, c);         vday = new jbutton("view day");         panel.add(vday, c);         vweek = new jbutton("view week");         panel.add(vweek, c);         task = new jbutton("assign/edit tasks");         panel.add(task, c);         exit = new jbutton("exit");         panel.add(exit, c);          frame.add(panel);         frame.pack();         frame.setvisible(true);         frame.setdefaultcloseoperation(jframe.exit_on_close);     }      public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  scheduler scheduler = new scheduler();                 scheduler.createframe();             }         });     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -