【发布时间】:2012-06-29 08:57:03
【问题描述】:
我想把 JLabel 放在 JPanel 的中心。我使用了下面的代码,所以你能告诉我。这段代码有什么问题? 代码:
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);
}
}
提前致谢
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖SSCCE。
-
还可以考虑了解LayoutManager,而不是自己使用 setLocation/setSize/setBounds 定位组件
标签: java swing jpanel jlabel layout-manager