Results 1 to 2 of 2
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    106
    Rep Power
    0

    Default 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.
    Java 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Aligning and Stretching components (i.e. JButton)

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with Stretching an Image
    By neptune692 in forum Java 2D
    Replies: 1
    Last Post: 07-16-2011, 05:04 AM
  2. Problem with aligning components
    By kedecr in forum New To Java
    Replies: 2
    Last Post: 03-09-2011, 01:23 PM
  3. aligning numbers,..
    By rexson98 in forum New To Java
    Replies: 6
    Last Post: 10-09-2008, 01:59 PM
  4. Having Trouble Aligning JLabels
    By Mark_Petrov in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2008, 05:22 PM
  5. Aligning Labels
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •