Sign in to follow this  
Flashthinker

[java] Displaying more than 1 component in JPanel??

Recommended Posts

I've been trying to solve this problem for the past 3 hours or so, and I just can't figure out what's wrong. Everything looks fine to me, I even compared it to other programs like it and it seems fine. So my questions is: How can I display more than one component in a JPanel? Here's my code so you can understand better what I'm talking about: (Basically, only the component that is in front (cir) of the other one (cir2) shows up... Even though I used setOpaque(false))
Quote:
package action; import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class Main extends JFrame { private Circle cir; private Circle cir2; private JPanel jpan; public Main() { final JFrame frame = this; jpan = new JPanel(); jpan.setLayout(new BorderLayout()); Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); this.setBounds(screensize.width/2-275,screensize.height/2-200,550,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cir = new Circle(100,100,100); cir2 = new Circle(200,200,200); jpan.add(cir2); jpan.add(cir); jpan.setBackground(new Color(1f,1f,1f)); this.setContentPane(jpan); jpan.addMouseMotionListener(new MouseListener(jpan,cir)); this.setVisible(true); } public static void main(String[] args) { new Main(); } } class MouseListener implements MouseMotionListener { private JPanel fr; private Circle cir; public MouseListener(JPanel fra, Circle ci) { fr = fra; cir = ci; } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { e.translatePoint(-150,-150); cir.setBounds(e.getX(),e.getY(),cir.getWidth(),cir.getHeight()); fr.repaint(); } } class Circle extends JComponent{ private int x; private int y; private int width; private int height; public Circle(int ax, int ay, int radius) { this.setOpaque(false); x = ax; y = ay; width = radius; height = radius; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.drawOval(x,y,width,height); } }
[Edited by - Flashthinker on June 2, 2007 2:34:46 AM]

Share this post


Link to post
Share on other sites
Currently you're adding two JComponent's to the same "slot" of the BorderLayout for jpan (I think it uses BorderLayout.CENTER by default). This causes cir2 to be replaced by cir.

You could either change jpan's Layout to something else, or you need to add another container in-between like this:



jpan = new JPanel();
jpan.setLayout(new BorderLayout());

//...

JPanel extraContainer = new JPanel(new FlowLayout());

cir = new Circle(100,100,100);
cir2 = new Circle(200,200,200);

extraContainer.add(cir2);
extraContainer.add(cir);

jpan.add(extraContainer);

Share this post


Link to post
Share on other sites
I solved the problem earlier today afer doing some more research... I swicthed to OverlayLayout(jpan) and it worked!

In summary; I underestimated the power of layout managers.
Thanks for the tips though, it looks likeI've got some more reading to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this