在绘制方法中调用JLabel时不显示

时间:2015-08-02 16:09:23

标签: java swing jlabel java-2d

我有以下代码:

public class Canvas extends JPanel{
 JLabel label = new JLabel();
 public void init()
    {
        label.setSize(100, 100);
        label.setLocation(10, 10);
        label.setText("lalallaalal");
        this.add(label);
    }
@Override
public void paint(Graphics g) {
        super.paint(g);

                paintRoad(g);
                paintBorders(g);

                paintEnemies(g, enemies);
                paintPlayer(g);
    }

我希望每次重新绘制JPanel时都会重新绘制标签,但是当我在this.add(label)方法的末尾放置paint时,它不会显示标签。 知道为什么吗?

2 个答案:

答案 0 :(得分:3)

paintComponent()调用paintComponent。最好覆盖paint而不是protected void paintComponent(Graphics g)

select Date1,  Date2
from 
    (SELECT Date1 FROM Table1 GROUP BY Date1) t1
left join
    (SELECT Date2 FROM Table2 GROUP BY Date2) t2
on
    t1.Date1 = t2.Date2

minHeight

答案 1 :(得分:3)

尝试在paint方法中使用 drawString(String str,int X,int y)方法,而不是使用JLabel。

public void paint(Graphics g){
    g.drawString(label.getText(), 110, 110);
}