Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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))
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);



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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!