Results 1 to 6 of 6
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default help with gridbaglayout

    This is my first attempt at gridbaglayout so don't laugh :D I am trying to layout a simple calculator that has 2 text fields and 4 buttons to choose the operator. My problem is its not working :)

    Here is what I want it to look like.


    Here is what mine looks like:


    Here is my code.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class Calculator extends JFrame {
    
        JButton add, subtract, multiply, divide;
        JTextField num1, num2;
        JLabel result, enter1, enter2;
    
        public Calculator() {
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            enter1 = new JLabel("1st: ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            add(enter1, c);
    
            num1 = new JTextField(10);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            c.gridwidth = 3;
            add(num1, c);
    
            enter2 = new JLabel("2nd: ");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            add(enter2, c);
    
            num2 = new JTextField(10);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            c.gridwidth = 3;
            add(num2, c);
    
            add = new JButton("+");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            add(add, c);
    
            subtract = new JButton("-");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 2;
            add(subtract, c);
    
            multiply = new JButton("*");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 2;
            add(multiply, c);
    
            divide = new JButton("/");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 3;
            c.gridy = 2;
            add(divide, c);
    
            result = new JLabel("");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            c.gridwidth = 4;
            add(result, c);
        }
    
        public static void main (String args[]) {
            Calculator gui = new Calculator();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            gui.setSize(250,150);
            gui.setTitle("Calculator");
        }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    The same constraint is used for every component. Since you never reset the gridwidth, I would guess the width of 3 is used for all components which is not what you want.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    OK... how do you go about resetting the grid width

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by robertbob View Post
    OK... how do you go about resetting the grid width
    You might want to check out the GridBagConstraints API to see if there is a field with a name similar to gridWidth. You might get lucky!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    OK... how do you go about resetting the grid width
    Seriously?

    What did you try? What problems did you have?

    Your reply is time stamped 4 minutes after my suggestion. If you would even think about this for a minute I'm sure you can figure it out.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    On further review, you may wish to mix your layouts here. Consider using BorderLayout for the main JPanel, then adding the buttons to a JPanel that uses GridLayout(1, 0) (1 row, variable columns), and adding this button panel to the main panel BorderLayout.SOUTH. Then the JLabel/JTextField panel can use GridBagLayout and be added to the main JPanel BorderLayout.CENTER.

Similar Threads

  1. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  2. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  3. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:16 AM
  4. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 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
  •