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: Help with Swing

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Help with Swing

    I am trying to make a calculator. I am just trying to setup the GUI first. The problem I am having is that I want the JTextField to be the full width of the pane above all the JButtons. I want the double zero button to take up the space of two buttons horizontally in the bottom left
    corner. I want the enter button to take up the space of two Jbuttons vertically in the bottom right hand corner. Here is a pic of what my UI looks like right now:
    javakeypad.jpg

    Here is my code:
    public class KeypadUse
    {
    	public static void main(String[] args)
    	{
    		Keypad key = new Keypad();
    	}
    }



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Keypad extends JFrame
    {
    	private JTextField displayTF;
    	private JButton oneB, twoB, threeB, fourB, fiveB, sixB, sevenB, eightB, nineB, zeroB,
    				doubleZeroB, plusB, minusB, enterB;
     
    	public Keypad()
    	{
    		setTitle("Keypad");
    		Container pane = getContentPane();
    		pane.setLayout(new GridLayout(5, 4));
     
    		displayTF = new JTextField(20);
    		oneB = new JButton();
    		twoB = new JButton();
    		threeB = new JButton();
    		fourB = new JButton();
    		fiveB = new JButton();
    		sixB = new JButton();
    		sevenB = new JButton();
    		eightB = new JButton();
    		nineB = new JButton();
    		zeroB = new JButton();
    		doubleZeroB = new JButton();
    		plusB = new JButton();
    		minusB = new JButton();
    		enterB = new JButton();
     
    		pane.add(displayTF);
    		pane.add(minusB);
    		pane.add(nineB);
    		pane.add(eightB);
    		pane.add(sevenB);
    		pane.add(plusB);
    		pane.add(sixB);
    		pane.add(fiveB);
    		pane.add(fourB);
    		pane.add(enterB);
    		pane.add(threeB);
    		pane.add(twoB);
    		pane.add(oneB);
    		pane.add(zeroB);
    		pane.add(doubleZeroB);
     
    		setSize(500,500);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
    }

    what I was thinking is that maybe there is a way to put a "place holder" to take up a full row and then I can resize my textfield. But I am
    stumped on the buttons.

    thanks in advance!


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Help with Swing

    use gridbag layout!! that will surely help!
    click here to know more!

Similar Threads

  1. Help with swing GUI
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 03:54 AM
  2. Could need some help with Swing
    By Kerr in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2011, 10:11 AM
  3. IO-swing problem
    By haresh2906 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 07:00 AM
  4. Swing
    By waboke in forum AWT / Java Swing
    Replies: 2
    Last Post: November 3rd, 2010, 08:55 AM
  5. Swing Timers
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 5
    Last Post: November 10th, 2009, 09:10 PM