It should come pretty easy, but it's not making sense why I cannot have a 500x300 pix frame and inside it, have two 250x500 JPanels (side by side). I was hard at it for about a week and didn't even come close. I cannot even add a small JPanel to a JFrame without the panel taking up the whole frame. In the following code, how can I add two TSPanels (side by side) to occupy one JFrame???
Vinod Venugopal
Ranch Hand
Joined: Dec 06, 2000
Posts: 148
posted
0
Hi Steven, A Frame's default layout is Border Layout, so if u dont specify any layout, it just adds components in the CENTER ( like whats happening in your case ), so what u can do is specify BorderLayout & add the 2 panels in CENTER & EAST/WEST , or use FlowLayout & maximize the Panels. Also I changed maximum size to preferred size. this is ur modified code: import java.awt.*; import javax.swing.*; class TSPanel extends JPanel { public TSPanel () { this.setBackground (Color.blue); Dimension d1 = new Dimension(250, 150); //this.setMaximumSize(d1); this.setPreferredSize(d1); } public void paintComponent (Graphics g) { super.paintComponent(g); g.drawString ("Hello World!", 100, 100); } } public class ts4copy { public static void main (String[] argv) { JFrame frame = new JFrame (); frame.setTitle ("Hello World Test"); frame.setResizable (true); frame.setSize (500, 300); TSPanel panel = new TSPanel (); TSPanel panel1 = new TSPanel (); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add (panel,BorderLayout.CENTER); frame.getContentPane().add (panel1,BorderLayout.WEST); frame.setVisible (true); } }
Should do it! Vinod
- Vinod<br />-------<br />SCJP2
Steven YaegerII
Ranch Hand
Joined: May 31, 2000
Posts: 182
posted
0
Thanks alot! I've been going batty trying to accomplish that very same thing. Looking back, I know I've come close more than once but I completely overlooked the layout managers. Thanks, Vinod, for getting me unstuck. SteveII
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
GridLayout would work very well for this. Just create a grid with 1 row and 2 columns and place your panels in the grid.
Steven YaegerII
Ranch Hand
Joined: May 31, 2000
Posts: 182
posted
0
That makes sense...gridlayout it is. Thanks, guys, for helping me out and also renewing my enthusiasm. SteveII