Aligning and Stretching components (i.e. JButton)
I am trying to setSize for the Buttons so that they will stretch across their respective cell of the GridBagLayout. Now, I have looked through the API and have hunted down a few setSize. I know it isn't Java, so what have I overlooked perhaps that has been causing me to pull my hair out.
Code:
import java.awt.*;
import javax.swing.*;
public class BillFlipper {
public static int winW = 400;
public static int winH = 850;
public static int items = 1; // Includes Title and all Flippers
public static int rowH = winH/items;
public static void addComponentsToPanel(Container pane){
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel title = new JLabel("Bill Swapper");
title.setFont(new Font("sansserif", Font.BOLD, 30));
title.setSize((winW-100), (winH/items));
counter();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
pane.add(title, c);
JLabel needL = new JLabel("NEED TO PAY |");
needL.setHorizontalAlignment(SwingConstants.CENTER);
needL.setFont(new Font("monospaced", Font.BOLD, 20));
needL.setSize(200, 250);
c.insets = new Insets(0,25,0,25);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
counter();
pane.add(needL, c);
JLabel paidL = new JLabel("| ALREADY PAID");
paidL.setHorizontalAlignment(SwingConstants.CENTER);
paidL.setFont(new Font("monospaced", Font.BOLD, 20));
paidL.setSize(200,250);
c.insets = new Insets(0,25,0,25);
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
pane.add(paidL, c);
Dimension buttons = new Dimension();
buttons.setSize(winW, (winH/items));
JButton but1 = new JButton("ALFA");
but1.setSize(buttons);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
pane.add(but1, c);
JButton but2 = new JButton("BALDWIN EMC");
but2.setSize(buttons);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 3;
pane.add(but2, c);
JButton but3 = new JButton("AT&T");
but3.setSize(buttons);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 4;
pane.add(but3, c);
JButton but4 = new JButton("DELL 1");
but4.setSize(buttons);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 5;
pane.add(but4, c);
}
public static void createAndShowGUI(){
Dimension minimumSize = new Dimension();
minimumSize.setSize(winW, winH);
JFrame frame = new JFrame("PAGE TITLE");
frame.setMinimumSize(minimumSize);
frame.setLocation(0, 0);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPanel(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static int counter(){
items++;
return(items);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}
The program has no errors, but I am just not grasping the setSize (as well as aligning components). Can someone smack me on the back of the head and point me in a general direction as to where I might have strayed.
Thanks
Re: Aligning and Stretching components (i.e. JButton)