Results 1 to 3 of 3
- 12-10-2008, 06:46 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
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!
- 12-10-2008, 07:26 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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);Neil Coffey
Javamex - Java tutorials and performance info
- 12-10-2008, 07:48 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
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
-
panel positioning
By shwein in forum New To JavaReplies: 4Last Post: 09-09-2008, 05:15 PM -
images, panels and applets
By jamesfrize in forum Java AppletsReplies: 3Last Post: 03-20-2008, 04:35 PM -
Buttons to show new panels
By Lehane_9 in forum AWT / SwingReplies: 1Last Post: 03-06-2008, 04:22 PM -
Working with Labels on Panels.
By vargihate in forum AWT / SwingReplies: 2Last Post: 01-04-2008, 04:09 AM -
Need a tutorial for Studying about Panels
By ramachandran in forum New To JavaReplies: 1Last Post: 10-25-2007, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks