Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Making a GridBagLayout Fill the Container

import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; public class Main {   public static void main(String[] argv) throws Exception {     JFrame frame = new JFrame();     GridBagLayout gbl = new GridBagLayout();     frame.setLayout(gbl);     frame.add(new JButton("1"));     frame.add(new JButton("2"));     gbl.layoutContainer(frame);     double[][] weights = gbl.getLayoutWeights();     for (int i = 0; i < 2; i++) {       for (int j = 0; j < weights[i].length; j++) {         weights[i][j] = 1;       }     }     gbl.columnWeights = weights[0];     gbl.rowWeights = weights[1];     frame.pack();     frame.setVisible(true);   } }