Possible Duplicate:
Centering a JLabel on a JPanel
i want to put JLabel in to the center of JPanel. i have used below code so can you just tell me. what is wrong with this code? code :
public class ColoredRect extends JPanel{
public double x, y, width, height;
public JLabel name;
public ColoredRect(double x,double y,String label)
{
this.x = x;
this.y = y;
this.width = 100;
this.height =40;
setLocation((int)x,(int)y);
setSize((int)width,(int)height);
setBackground(Color.red);
name = new JLabel(label,JLabel.CENTER);
name.setForeground(Color.BLACK);
name.setVisible(true);
name.setSize(20,20);
name.repaint();
add(name);
}
}
thanks in advance