Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using multiple JPanels

843804Feb 9 2005 — edited Feb 9 2005
I'm fairly new to Java, and I've been playing around with Swing. I put two JPanels into a JFrame, which apparantly align themselves side-by-side by default. I then created two text boxes and put them into the left panel and the right panel respectively. The first text box shows up as expected. The text box in the right panel, however, 'hides' under the left panel, and in order to get it to appear fully on the right panel I have to maximize the window. I did some other testing with this, and if I draw on the right panel (using the Graphics class) at the origin it appears in the upper left corner of the window (on the left panel).

Here's the code I'm using:
import javax.swing.*;
import java.awt.*;

public class test {
 public static void main(String[] args) {
  JFrame frame = new JFrame();
  JPanel p1 = new JPanel();
  JPanel p2 = new JPanel();
  
  p1.setSize(500, 500);
  p2.setSize(200, 500);
  frame.setSize(700, 500);

  JLabel ltext = new JLabel("Left Panel Text");
  JLabel rtext = new JLabel("Right Panel Text");
  p1.add(ltext);
  p2.add(rtext);

  frame.getContentPane().add(p1);
  frame.getContentPane().add(p2);
  frame.show();
 }
}
Is this how panels are supposed to act -- not putting things on their own visible area -- or am I doing something wrong (probably more likely)?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 9 2005
Added on Feb 9 2005
2 comments
1,370 views