Results 1 to 3 of 3
  1. #1
    javatard is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default I obviously don't understand positioning panels

    I suck so so badly at Java. I can't even grasp this simple concept

    All Im trying to do is create a frame, then create a panel inside it and set the size and position of that panel, THATS IT
    I have created a border on the panel and instead of the border showing around the sized panel, it shows around the whole frame! I dont know whats going on

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    
    
    public class RushHour
    {
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				RushHourFrame frame = new RushHourFrame();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setVisible(true);
    			}
    		});
    	}
    }
    
    class RushHourFrame extends JFrame
    {
    	public RushHourFrame()
    	{
    		setTitle("Super Happy Fun Time Rush Hour Clone");
    		setBounds(300,300, 300, 400);
    		
    		// Create panel
    		gridPanel grid = new gridPanel();
    		
    		grid.setBounds(20,20,200,200);
    		grid.setBorder(BorderFactory.createLineBorder(Color.pink));
    		
    		add(grid);
    	}
    
    
    }
    
    class gridPanel extends JPanel
    {
    	public void paintComponent( Graphics g ) 
    	{
    		//super.paintComponent( g );
    	    // Draw the vertical and horizontal lines:
    		for(int i=0; i<rows; i++)
    		{
    			g.drawLine(i*cellsize,0, i*cellsize,(rows-1)*cellsize);
    			g.drawLine(0,i*cellsize, (rows-1)*cellsize,i*cellsize);
    		}
    	}
    	
    	private final int rows = 6;
    	private final int cellsize = 40;
    }

    Help is immensely appreciated!

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Frames (and containers in general-- e.g. JPanels) have a layout that dictates how things are positioned inside them. You don't normally set specific hard-coded coordinates with setBounds() (though with a slight modification you can).

    The problem here is that by default, a JFrame has a layout of type BorderLayout. BorderLayout expects to handle the exact coordinates of the child components (the panel in this case) itself -- i.e. it won't take notice of your call to setBounds(). BorderLayout lays out your components in terms of fixed locations: NORTH, SOUTH, EAST, WEST and CENTER. Here it's taking your component as the CENTER component and making it fill the frame (because you haven't told it anything different).

    So you have a couple of options:
    (1) Tell the JFrame not to use any particular layout -- call setLayout(null) on it.
    (2) Arguably the preferred option -- just let the component fill the frame, or use another more appropriate layout to suit your needs. If you do this, it's best to call explicitly:

    add(grid, BorderLayout.CENTER);

  3. #3
    javatard is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your reply Neil
    I wasn't aware JFrame defaulted to BorderLayout, things at least make a little more sense now! (I thought it was FlowLayout like JFrames, I was wrong).

    So when I setLayout to null, things work as I intended
    When I use BorderLayout, things are not working

    Here is the changed code

    Java Code:
    class RushHourFrame extends JFrame
    {
    	public RushHourFrame()
    	{
    		setTitle("Super Happy Fun Time Rush Hour Clone");
    		setBounds(300,300, 300, 400);
    		// By default, frame has BorderLayout manager
    		
    		// Create panel
    		JPanel centerPanel = new JPanel();
    		add(centerPanel, BorderLayout.CENTER);
    		
    		gridPanel grid = new gridPanel();
    		grid.setSize(200,200);
    		grid.setBorder(BorderFactory.createLineBorder(Color.pink));
    		
    		centerPanel.add(grid);
    	}
    }

    The border is no longer around the whole frame but its now just a tiny square near the top :confused:

Similar Threads

  1. panel positioning
    By shwein in forum New To Java
    Replies: 4
    Last Post: 09-09-2008, 06:15 PM
  2. images, panels and applets
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 03-20-2008, 05:35 PM
  3. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:22 PM
  4. Working with Labels on Panels.
    By vargihate in forum AWT / Swing
    Replies: 2
    Last Post: 01-04-2008, 05:09 AM
  5. Need a tutorial for Studying about Panels
    By ramachandran in forum New To Java
    Replies: 1
    Last Post: 10-25-2007, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •