自定义JLabel画一个圆圈

时间:2011-09-11 15:25:20

标签: java

我想要一个围绕它绘制圆圈的JLabel。我已经有了一些代码,但圆圈没有绘制,我在这里做错了吗?

public class TowerMarker extends JLabel
{
    private int x, y, id;
    private ImageIcon ic;
    private String ql;

    public TowerMarker(int id, int x, int y, ImageIcon ic, String ql)
    {
        this.x = x;
        this.y = y;
        this.ic = ic;
        this.ql = ql;
        setBounds(x, y, 40, 85);
        setIcon(ic);
        setToolTipText("QL: "+ql);
        updateUI();

    }

    @Override
    public int getX()
    {
            return x;
    }

    @Override
    public int getY()
    {
        return y;
    }

    public String getQL()
    {
        return ql;
    }

    public ImageIcon getImageIcon()
    {
        return ic;
    }

    public int getID()
    {
        return id;
    }

    @Override
    public String toString()
    {
        return x+":"+y+":"+ql;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        g.setColor(Color.red);

        g.drawOval(x, y, 20, 20);

        ui.update(g, this);
    }
}

1 个答案:

答案 0 :(得分:0)

致电super.paintComponent(g);

 @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.red);

        g.drawOval(x, y, 20, 20);

        ui.update(g, this);
    }

但不使用paintComponent

public void paint( Graphics g )