Results 1 to 4 of 4
  1. #1
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default trouble with layout managers

    ok, so here my new problem. Originally, I used the getContentPane of JFrame, set it to GridLayout, and added my class SquarePanel, which extends JPanel and has a paintComponent method that looks like this:

    Java Code:
    public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		int width = getWidth();
    		int height = getHeight();
    		
    		if(color == Colors.WHITE)
    			g.setColor(Color.white);
    		else
    			g.setColor(Color.green);
    		
    		g.fillRect(0, 0, width, height);
    		g.setColor(Color.black);
    		//System.out.println("here");
    	}
    this worked fine and produced an 8x8 grid of different colored squares that was sized correctly. Now, the next step in my program is to add more components(this is a chess program) to the main frame, other than having the grid fill up the whole frame. In order to do this I first tried to make the contentPanes layout manager flowlayout, add a label on the left, and add my panel of squares to the right. the result was the squarepanels were all squished over to the right, and appeared as a really thin tall black rectangle, which i have attached. I read the documentation, and it said gridlayout resizes, not sure why it resized in such an extreme way, but it did so my next step was to try 8 panels of squares, each with flowlayout, and then add all 8 panels to another panel with boxlayout that stacked vertically. I read that both of these do not resize, but when i ran it, the result was the squares all appeared as tiny dots, rather than the size they should be. also, here is the code of my constructor for my main JFrame. Its long, but mostly repetitive. I did as much research as I can on layout managers, but havnt seen anything that could be casuing this issue. I added in some comments as to why I tried certain things

    Java Code:
    public GameFrame(Game game) {
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container con = this.getContentPane();
    		setSize(1000,800);
    		setLocation(0,0);
    		SquarePanel pane = null;
    		SquarePanel lastpanel = null;
    		squares = new SquarePanel[8][8];
    		int color = Colors.WHITE;
    		JPanel main = new JPanel();
    		main.setLayout(new FlowLayout());
    		JPanel panelright = new JPanel();
    		panelright.setLayout(new BoxLayout(panelright, BoxLayout.Y_AXIS));
    		JPanel panelleft = new JPanel();
    		panelleft.setSize(200,800);
    		panelright.setSize(800,800);
    		panelright.setMaximumSize(new Dimension(800,800));
    		JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8;
    		panel1 = new JPanel();
    		panel1.setLayout(new FlowLayout());
    		panel2 = new JPanel();
    		panel2.setLayout(new FlowLayout());
    		panel3 = new JPanel();
    		panel3.setLayout(new FlowLayout());
    		panel4 = new JPanel();
    		panel4.setLayout(new FlowLayout());
    		panel5 = new JPanel();
    		panel5.setLayout(new FlowLayout());
    		panel6 = new JPanel();
    		panel6.setLayout(new FlowLayout());
    		panel7 = new JPanel();
    		panel7.setLayout(new FlowLayout());
    		panel8 = new JPanel();
    		panel8.setLayout(new FlowLayout());
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,0);		
    			pane = lastpanel;
    			panel1.add(pane);
    			squares[j][0] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,1);	
    			pane = lastpanel;			
    			panel2.add(pane);
    			squares[j][1] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,2);		
    			pane = lastpanel;
    			panel3.add(pane);
    			squares[j][2] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,3);		
    			pane = lastpanel;
    			panel4.add(pane);
    			squares[j][3] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,4);			
    			pane = lastpanel;
    			panel5.add(pane);
    			squares[j][4] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,5);	
    			pane = lastpanel;
    			panel6.add(pane);
    			squares[j][5] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,6);		
    			pane = lastpanel;
    			panel7.add(pane);
    			squares[j][6] = pane;
    		}
    		
    		if(color == Colors.WHITE)
    			color = Colors.BLACK;
    		else
    			color = Colors.WHITE;
    				
    		for(int j = 0; j <= 7; ++j) {
    		
    			if(color == Colors.WHITE)
    				color = Colors.BLACK;
    			else
    				color = Colors.WHITE;
    			
    			lastpanel = new SquarePanel(color,j,7);	
    			pane = lastpanel;
    			panel8.add(pane);
    			squares[j][7] = pane;
    		}
    	
    		Board board = game.getBoard();
    		
    		//this bit of code just sets up chess peices on each square. this is confirmed to work
    		for(int i = 0; i < 8; ++i) {
    			for(int j = 0; j < 8; ++j) {
    				squares[j][i].setResident(board.getSquare(j,i).getResident());
    				//System.out.println(squares[j][i].getResident());
    				//squares[j][i].repaint();
    			}
    		}
    		
    		MouseListener listener = new MoveListener(game, this, squares);
    		for(int i = 0; i < 8; ++i)
    			for(int j = 0; j < 8; ++j)
    				squares[i][j].addMouseListener(listener);
    		
    		panelright.add(panel1);
    		panelright.add(panel2);
    		panelright.add(panel3);
    		panelright.add(panel4);
    		panelright.add(panel5);
    		panelright.add(panel6);
    		panelright.add(panel7);
    		panelright.add(panel8);
    		panelright.setVisible(true);
    		panelleft.setVisible(true);
    		panelleft.add(new JLabel("this is a test"));
    		main.add(panelleft);
    		main.add(panelright);
    		con.add(main);
    		setVisible(true);
    		
    		// this is a last resort to get squares to show up correctly
    		for(int i = 0; i < 8; ++i) {
    			for(int j = 0; j < 8; ++j) {
    				squares[j][i].repaint();
    			}
    		}
    	}

  2. #2
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: trouble with layout managers

    ok, ive been playing around with it a bit, and it appears that if i add the statement "add(new JLabel(" "));" to each of the square panels, I can get them to appear, but small. I guess my real question is, why is the layout manager shrinking the square panels to their smallest possible display point? I posted the paintComponents method above of these square panels, which paints a 100x100 rectangle onto the panel, but the layout manager ignores this. it only looks at the blank jlabels added on. how can i get the layout manager to accept an empty panel, with no components, but that does have a valid pain component method? In my original program, I set the main contentPanes layout manager to gridlayout and added directly to that, and it worked fine. its only now when i make the main contentPanes layout flow layout, add a component on the left, and a component on the right, and make the right component a panel with gridlayout that the panels get compressed. also, my strategy of adding flow layouts to box layouts doesnt work because when you click on a square, i have it so that a border is painted around it, and when you do this, there is a gap between all the panels to make way for the border. there has to be a way to make this work with gridlayout
    Last edited by yemista; 10-27-2011 at 02:43 PM.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: trouble with layout managers

    Here is my little chess board example that might help you out:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
    {
    	JLayeredPane layeredPane;
    	JPanel chessBoard;
    	JLabel chessPiece;
    	int xAdjustment;
    	int yAdjustment;
    
    	public ChessBoard()
    	{
    		Dimension boardSize = new Dimension(600, 600);
    
    		//  Use a Layered Pane for this this application
    
    		layeredPane = new JLayeredPane();
    		layeredPane.setPreferredSize( boardSize );
    		layeredPane.addMouseListener( this );
    		layeredPane.addMouseMotionListener( this );
    		getContentPane().add(layeredPane);
    
    		//  Add a chess board to the Layered Pane
    
    		chessBoard = new JPanel();
    		chessBoard.setLayout( new GridLayout(8, 8) );
    		chessBoard.setPreferredSize( boardSize );
    		chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
    		layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
    
    		//  Build the Chess Board squares
    
    		for (int i = 0; i < 8; i++)
    		{
    			for (int j = 0; j < 8; j++)
    			{
    				JPanel square = new JPanel( new BorderLayout() );
                	square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
    				chessBoard.add( square );
    			}
    		}
    
    		// Add a few pieces to the board
    
    		ImageIcon duke = new ImageIcon("dukewavered.gif"); // add an image here
    
    		JLabel piece = new JLabel( duke );
    		JPanel panel = (JPanel)chessBoard.getComponent( 0 );
    		panel.add( piece );
    		piece = new JLabel( duke );
    		panel = (JPanel)chessBoard.getComponent( 15 );
    		panel.add( piece );
    	}
    
    	/*
    	**  Add the selected chess piece to the dragging layer so it can be moved
    	*/
    	public void mousePressed(MouseEvent e)
    	{
    		chessPiece = null;
    		Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
    
    		if (c instanceof JPanel) return;
    
    		Point parentLocation = c.getParent().getLocation();
    		xAdjustment = parentLocation.x - e.getX();
    		yAdjustment = parentLocation.y - e.getY();
    		chessPiece = (JLabel)c;
    		chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
    
    		layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    		layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
    	}
    
    	/*
    	**  Move the chess piece around
    	*/
    	public void mouseDragged(MouseEvent me)
    	{
    		if (chessPiece == null) return;
    
    		//  The drag location should be within the bounds of the chess board
    
    		int x = me.getX() + xAdjustment;
    		int xMax = layeredPane.getWidth() - chessPiece.getWidth();
    		x = Math.min(x, xMax);
    		x = Math.max(x, 0);
    
    		int y = me.getY() + yAdjustment;
    		int yMax = layeredPane.getHeight() - chessPiece.getHeight();
    		y = Math.min(y, yMax);
    		y = Math.max(y, 0);
    
    		chessPiece.setLocation(x, y);
    	 }
    
    	/*
    	**  Drop the chess piece back onto the chess board
    	*/
    	public void mouseReleased(MouseEvent e)
    	{
    		layeredPane.setCursor(null);
    
    		if (chessPiece == null) return;
    
    		//  Make sure the chess piece is no longer painted on the layered pane
    
    		chessPiece.setVisible(false);
    		layeredPane.remove(chessPiece);
    		chessPiece.setVisible(true);
    
    		//  The drop location should be within the bounds of the chess board
    
    		int xMax = layeredPane.getWidth() - chessPiece.getWidth();
    		int x = Math.min(e.getX(), xMax);
    		x = Math.max(x, 0);
    
    		int yMax = layeredPane.getHeight() - chessPiece.getHeight();
    		int y = Math.min(e.getY(), yMax);
    		y = Math.max(y, 0);
    
    		Component c =  chessBoard.findComponentAt(x, y);
    
    		if (c instanceof JLabel)
    		{
    			Container parent = c.getParent();
    			parent.remove(0);
    			parent.add( chessPiece );
    			parent.validate();
    		}
    		else
    		{
    			Container parent = (Container)c;
    			parent.add( chessPiece );
    			parent.validate();
    		}
    	}
    
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseMoved(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    
    	public static void main(String[] args)
    	{
    		JFrame frame = new ChessBoard();
    		frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
    		frame.setResizable( false );
    		frame.pack();
    		frame.setLocationRelativeTo( null );
    		frame.setVisible(true);
    	 }
    }

  4. #4
    yemista is offline Member
    Join Date
    Oct 2011
    Posts
    41
    Rep Power
    0

    Default Re: trouble with layout managers

    thank you, your code helped alot, though i still dont understand where i went wrong

Similar Threads

  1. Need help with JFrame and Managers.
    By Martino in forum New To Java
    Replies: 1
    Last Post: 04-22-2011, 11:20 PM
  2. set button size in case of layout managers
    By silversurfer2in in forum AWT / Swing
    Replies: 3
    Last Post: 06-22-2010, 10:23 PM
  3. Why use layout managers in applet?
    By Splat in forum New To Java
    Replies: 2
    Last Post: 10-14-2009, 04:41 AM
  4. Multiple Layout Managers
    By AJG in forum New To Java
    Replies: 0
    Last Post: 04-06-2008, 01:43 AM
  5. Layout Managers
    By gmioannou in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 04:12 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
  •