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 3 of 3

Thread: Gap between GUI elements

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Gap between GUI elements

    Hey, I'm using the following code to make an interface, but for some reason there is a massive gap between the text fields and the next panel. Any idea how I can fix this and make it a smaller gap?

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
     
    class MyFrame extends JFrame
    {
    	public MyFrame()
    	{
    		//frame settings
    		setVisible (true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("trololo");
    		setLocation(300,300);
    		setSize(300, 300);
     
    		//declarations
    		JButton[][] Keypad = new JButton[4][4];
    		String[][] KeypadLabels = {{"7","4","1","0"},{"8","5","2","C"},{"9","6","3","!"},{"+","-","*","="}};
    		JTextField ScreenOut = new JTextField("asd");
    		JTextField ScreenIn = new JTextField("asd");
    		JMenuBar Toolbar = new JMenuBar();
    		JMenu Options = new JMenu("Options");
    		JPanel Section1 = new JPanel();
    		JPanel Section2 = new JPanel();
    		JPanel Section3 = new JPanel();
     
     
    		//containers
    		java.awt.Container c = getContentPane();
    		c.setLayout(new java.awt.GridLayout(3,1, 0, 0));
    		Section1.setLayout(new java.awt.FlowLayout());
    		Section1.add(ScreenIn);
    		Section2.setLayout(new java.awt.GridLayout(4,4));
    		Section3.setLayout(new java.awt.FlowLayout());
    		Section3.add(ScreenOut);
    		c.add(Section1);
    		c.add(Section2);
    		c.add(Section3);
    		Toolbar.add(Options);
    		setJMenuBar(Toolbar);
     
     
    		//keypad
    		int count =0;
    		for (int col =0; col<4;col++)
    		{
    			for (int row = 0; row<4;row++)
    			{
    				Keypad[col][row] = new JButton("");
    				Section2.add(Keypad[col][row]);;
    				Keypad[col][row].setLabel(KeypadLabels[row][col]);
    				count++;
    			}
    		}
    	}
    }

    class calculator
    {
    	public static void main(String Args[])
    	{
    		MyFrame f1 = new MyFrame();
    	}
    }

    Thanks in advance


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Gap between GUI elements

    Layout managers can look pretty daunting at first, there are so many to choose from - I recommend reading up on BorderLayout first, it is often the most useful top-level layout for GUIs like yours, and it is a lot simpler to use than GridLayout (unless you really need a grid).

Similar Threads

  1. Duplicating Elements through Recursion
    By vk999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 12:22 AM
  2. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 05:38 PM
  3. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  4. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  5. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 05:11 PM