Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Java Swing ( GridBagLayout )

  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Java Swing ( GridBagLayout )

    Hello,
    I am new to Java Swing and I am currently working on my own project attempting to make a GUI calculator.

    Could someone please explain to me how I can resize my operator Jbuttons ( /,+,*, -) so that they are the same size as the number JButtons( 1 - 9) and Also how i can align my buttons correctly.

    Thanks :)

    Here is my code:

    Java Code:
    import java.util.Stack;
    import java.io.*;  
    import javax.swing.*; 
    import java.util.Scanner;
    import java.awt.*;
    
    public class Calculator2{
      
      
      
      public Calculator2(){}
      
      public void Interface(){
       
        JFrame frame = new JFrame("Calculator 2.0");
        frame.setSize(240,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
       
        JMenu menu2 = new JMenu("Exit");
        menuBar.add(menu2);
        
        
        
        JPanel panel = new JPanel(new GridBagLayout());
        frame.getContentPane().add(panel, BorderLayout.NORTH);
        GridBagConstraints a = new GridBagConstraints();
        a.anchor = GridBagConstraints.NORTHWEST;
        
        
        
        a.insets = new Insets(2,2,2,2); 
        
        JLabel label = new JLabel("hello");
        a.gridx = 0; 
        a.gridy = 0;
         
        
        label.setBackground(Color.lightGray);
        label.setOpaque(true);
        panel.add(label,a);
        
        
        a.weightx = 0.5; 
        JButton bttn0 = new JButton("7");
        a.gridx = 0;
        a.gridy = 1;
        
        panel.add(bttn0,a);
        
        JButton bttn1 = new JButton("8");
        a.gridx = 1;
        a.gridy = 1;
        panel.add(bttn1,a);
        
        JButton bttn2 = new JButton("9");
        a.gridx = 2;
        a.gridy = 1;
        panel.add(bttn2,a);
        
        JButton bttn3 = new JButton("4");
        a.gridx = 0;
        a.gridy = 2;
        panel.add(bttn3,a);
        
        JButton bttn4 = new JButton("5");
        a.gridx = 1;
        a.gridy = 2;
        panel.add(bttn4,a); 
        
        JButton bttn5 = new JButton("6");
        a.gridx = 2;
        a.gridy = 2;
        panel.add(bttn5,a);
        
        JButton bttn6 = new JButton("1");
        a.gridx = 0;
        a.gridy = 3;
        panel.add(bttn6,a);
        
        JButton bttn7 = new JButton("2");
        a.gridx = 1;
        a.gridy = 3;
        panel.add(bttn7,a);
        
        JButton bttn8 = new JButton("3");
        a.gridx = 2;
        a.gridy = 3;
        panel.add(bttn8,a);
        
      JButton bttn9 = new JButton("0");
        a.gridx = 0;
        a.gridy = 4;
         a.gridwidth = GridBagConstraints.RELATIVE;
        bttn9.setPreferredSize(new Dimension(92,26));
        panel.add(bttn9,a);
       
        JButton btn1 = new JButton(".");
       
        a.gridx = 2;
        a.gridy = 4;
        panel.add(btn1,a);
        
        JButton btn2 = new JButton("/");
       
        a.gridx = 3;
        a.gridy = 1;
        panel.add(btn2,a);
        
        JButton btn3 = new JButton("*");
       
        a.gridx = 3;
        a.gridy = 2;
        panel.add(btn3,a);
        
        JButton btn4 = new JButton("-");
       
        a.gridx = 3;
        a.gridy = 3;
        panel.add(btn4,a);
        
        JButton btn5 = new JButton("+");
       
        a.gridx = 3;
        a.gridy = 4;
        panel.add(btn5,a);
      }
      
      public static void main(String[] args){
        Calculator2 calc = new Calculator2();
        calc.Interface();
        
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java Swing ( GridBagLayout )

    You're forgetting the fill constraint:

    Java Code:
          a.weightx = 0.5;
          a.fill = GridBagConstraints.BOTH;
    Also, you're setting the JFrame's size, and you should avoid doing that but rather letting the components decide on their best size by calling pack() on the JFrame after adding components. Also you will want to call setVisible(true) on the JFrame *after* adding the components to the GUI and in fact after calling pack(), not before adding components as you're doing.
    Dex likes this.

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

    Default Re: Java Swing ( GridBagLayout )

    Moved from New to Java

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

Similar Threads

  1. Trouble displaying an image using GridBagLayout
    By treeface99 in forum AWT / Swing
    Replies: 0
    Last Post: 03-15-2012, 09:52 PM
  2. GridBagLayout Help
    By Zman3359 in forum AWT / Swing
    Replies: 0
    Last Post: 02-21-2011, 08:39 PM
  3. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  4. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM
  5. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 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
  •