Skip to main content

Adding panels into a panel

1 reply [Last post]
kcakir
Offline
Joined: 2009-12-07
Points: 0

I am trying to develop a specific panel which includes 3 panels and one of them also includes 5 panels but only one of them seen on the screen
the code:

import java.awt.Color;
import java.awt.Container;
//import java.awt.LayoutManager;
import javax.swing.JPanel;

public class MiddlePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel stonearea1;
private JPanel stonearea2;
private JPanel seriarea;
private JPanel seri1;
private JPanel seri2;
private JPanel seri3;
private JPanel seri4;
private JPanel seri5;

public MiddlePanel(int x,int y,int w,int h) {
super();
// TODO Auto-generated constructor stub

this.setBounds(x, y, w, h);
this.setLayout(null);
this.initComp();
}

private void initComp(){
System.out.println("x "+this.getX()+"y "+this.getY()+"wid "+this.getWidth()+"h "+this.getHeight());
this.stonearea1 = new JPanel();
stonearea1.setBounds(this.getX(),this.getY(),this. getWidth()/3,this.getHeight());
stonearea1.setToolTipText("stonearea1");
stonearea1.setBackground(Color.black);
System.out.println("stone1 "+this.stonearea1.getHeight());
this.stonearea2 = new JPanel();
stonearea2.setBounds(this.getX()+this.stonearea1.g etWidth(), this.getY(), this.getWidth()/3,this.getHeight());
stonearea2.setToolTipText("stonearea2");
stonearea2.setBackground(Color.red);
System.out.println("stone2 "+this.stonearea2.getX()+" y "+this.stonearea2.getY());
this.seriarea = new JPanel(null);
seriarea.setToolTipText("seriarea");
seriarea.setBackground(Color.yellow);
seriarea.setBounds(this.getX()+this.stonearea1.get Width()+this.stonearea2.getWidth(),this.getY(),thi s.getWidth()/3,this.getHeight());
this.seri1 = new JPanel();
seri1.setBounds(seriarea.getX(), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4);
seri1.setToolTipText("seri1");
this.seri2 = new JPanel();
seri2.setBounds(seriarea.getX()+(seriarea.getWidth ()/4), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4);
seri1.setToolTipText("seri2");
this.seri3 = new JPanel();
seri3.setBounds(seriarea.getX()+(seriarea.getWidth ()/2), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4);
seri1.setToolTipText("seri3");
this.seri4 = new JPanel();
seri4.setBounds(seriarea.getX()+3*(seriarea.getWid th()/4), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4);
seri1.setToolTipText("seri4");
this.seri5 = new JPanel();
seri5.setBounds(seriarea.getX(),seriarea.getY()+3* (seriarea.getHeight()/4), seriarea.getWidth(), seriarea.getHeight()/4);
seri5.setToolTipText("seri1");
this.seriarea.add(seri1);
this.seriarea.add(seri2);
this.seriarea.add(seri3);
this.seriarea.add(seri4);
this.seriarea.add(seri5);
System.out.println("seriarea count "+this.seriarea.getComponentCount());

this.add(stonearea1);
this.add(stonearea2);
this.add(seriarea);

this.setVisible(true);
System.out.println("count "+this.getComponentCount());
}

}

On the screen only stonarea1 is shown. !!!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sauvage
Offline
Joined: 2004-03-04
Points: 0

Hi,

Bounds of a component are relative to its parent container, so IMO you should not use this.getX() value to set a child x position.
Furthermore you should use LayoutManagers to achieve the layout of components, this is the usual way of doing swing programming.
An IDE like NetBeans could help you to graphically design the layout, instead of coding it by yourself the code will be automatically generated.

Regards,

Laurent.