Results 1 to 6 of 6
Thread: help with gridbaglayout
- 05-18-2010, 12:48 AM #1
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
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"); } }
- 05-18-2010, 01:05 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 05-18-2010, 01:09 AM #3
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
OK... how do you go about resetting the grid width
- 05-18-2010, 01:12 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-18-2010, 02:35 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Seriously?OK... how do you go about resetting the grid width
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.
- 05-18-2010, 04:14 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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
-
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
GridBagLayout
By MuslimCoder in forum New To JavaReplies: 1Last Post: 01-15-2009, 08:54 PM -
GridBagLayout
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:16 AM -
GridBagLayout...please help
By newtojava7 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:16 AM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks