Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GridBagLayout can't seem to get it right.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GridBagLayout can't seem to get it right.

    Hello. I am trying to get some practice with Java GUI and using GridBagLayout.

    I am trying to create a simple calculator applicatoin, and I am having problems getting the buttons just right. So in the code below, I've only made visible a handful of the buttons, but it illustrates the problem that I am having.

    I want my calculator to look like the following:

    textfield
    1 2 3 +

    where the buttons 1, 2, and 3 are of width 3, + is width 1, and textfield spans the entire width of the application.

    The current code, when it displays, shows that buttons 1,2,3,+ all have the same wdith, yet I didn't specify that in the grid. Can someone find the problem in my code?

    Maybe more importantly, should I be using GridBagLayout to do what I want? Or should I just take complete control of the layout?

    thanks.

    And now the code....


    /* Calculator.java
    */


    import java.awt.Container;
    import java.awt.Component;
    import java.awt.event.ActionListener;
    import java.awt.Insets;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;

    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.BoxLayout;
    import java.awt.BorderLayout;

    class Calculator {
    JFrame frame;
    JTextField textfield;
    JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt0;
    JButton btDecimal, btEquals;
    JButton btPlus, btMinus, btMultiply, btDivide;
    final static Insets insets = new Insets(0,0,0,0);

    public Calculator() {
    frame = new JFrame( "Calculator");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    frame.setLayout( new GridBagLayout());

    textfield = new JTextField(15);
    bt1 = new JButton("1");
    bt2 = new JButton("2");
    bt3 = new JButton("3");
    bt4 = new JButton("4");
    bt5 = new JButton("5");
    bt6 = new JButton("6");
    bt7 = new JButton("7");
    bt8 = new JButton("8");
    bt9 = new JButton("9");
    bt0 = new JButton("0");

    btDecimal = new JButton( ".");
    btEquals = new JButton( "=");

    btPlus = new JButton( "+");
    btMinus = new JButton( "-");
    btMultiply = new JButton( "*");
    btDivide = new JButton( "/");

    addComponent( frame, textfield, 0,0, 10, 1);

    addComponent( frame, bt1 , 0,1, 3, 1);
    addComponent( frame, bt2 , 3,1, 3, 1);
    addComponent( frame, bt3 , 6,1, 3, 1);
    addComponent( frame, btPlus , 9,1, 1, 1);

    frame.pack();
    //frame.setSize(300,300);
    frame.setVisible(true);
    }

    public void addComponent( Container container, Component component,
    int gridx, int gridy, int widthx, int widthy)
    {
    GridBagConstraints gbc;
    gbc = new GridBagConstraints
    ( gridx, gridy, widthx, widthy, 1.0, 1.0,
    GridBagConstraints.CENTER,
    GridBagConstraints.BOTH,
    insets, 0, 0);

    container.add( component, gbc);
    return;
    }
    }


    public class CalculatorEngine{
    public static void main( String[] args) {
    Calculator gui = new Calculator();
    CalculatorEngine eng = new CalculatorEngine();

    return;
    }
    }


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GridBagLayout can't seem to get it right.

    Sorry about that very first post. Again I am trying to make a calculator and I want the numbers to be fatter buttons than the operators like '+'. However, the GridBagLayout Manager always seems to want to resize the buttons to be the same size.

    So below is much shortened version of the problem code. Notice that when I addComponent(), I specify that the size of the number pads should be 3 times the width of the plus button. But when you run this code, you'll see that the buttons arre all the same width.

    Any suggestions? Thanks.





    /* CalculatorEngine.java
     */
     
     
    import java.awt.Container;
    import java.awt.Component;
    import java.awt.event.ActionListener;
    import java.awt.Insets;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
     
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.BoxLayout;
    import java.awt.BorderLayout;
     
    class Calculator {
        JFrame frame;
        JTextField textfield;
        JButton bt1, bt2, bt3;
        JButton btPlus;
     
        final static Insets insets = new Insets(0,0,0,0);
     
        public Calculator() {
    	frame = new JFrame( "Calculator");
    	frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    	frame.setLayout( new GridBagLayout());
     
    	textfield = new JTextField(15);
    	bt1 = new JButton("1");
    	bt2 = new JButton("2");
    	bt3 = new JButton("3");
    	btPlus = new JButton( "+");
     
    	addComponent( frame, textfield, 0,0, 10, 1);
     
    	addComponent( frame, bt1      , 0,1, 3,  1);
    	addComponent( frame, bt2      , 3,1, 3,  1);
    	addComponent( frame, bt3      , 6,1, 3,  1);
    	addComponent( frame, btPlus   , 9,1, 1,  1);
     
    	frame.pack();
    	//frame.setSize(300,300);
    	frame.setVisible(true);
        }
     
        public void addComponent( Container container, Component component, 
    			      int gridx, int gridy, int widthx, int widthy)
        {
    	GridBagConstraints gbc;
    	gbc = new GridBagConstraints
    	    ( gridx, gridy, widthx, widthy, 1.0, 1.0,
    	      GridBagConstraints.CENTER,
    	      GridBagConstraints.BOTH,
    	      insets, 0, 0);
     
    	container.add( component, gbc);
    	return;
        }
    }
     
     
    public class CalculatorEngine{
        public static void main( String[] args) {
    	Calculator gui = new Calculator();
    	CalculatorEngine eng = new CalculatorEngine();
     
    	return;
        }
    }

Similar Threads

  1. GridBagLayout help
    By mjpam in forum AWT / Java Swing
    Replies: 1
    Last Post: May 8th, 2011, 10:51 AM
  2. GridBagLayout display problem
    By mjpam in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 5th, 2011, 04:58 PM
  3. GridBagLayout help
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 06:35 PM
  4. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 09:57 AM
  5. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 12:58 PM