对于自定义JLabel(形状已更改),setbackground方法绘制矩形而不是新形状

时间:2013-11-20 13:49:27

标签: java swing jlabel paintcomponent setbackground

我通过覆盖paintComponent()方法创建了一个自定义标签,如下所示。 但是当我在这个组件上调用setBackground()方法时。它绘制整个矩形。 我希望它只绘制自定义形状。请帮助。

自定义标签代码:

public class CustomLabel extends JLabel {
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Rectangle rect = g.getClipBounds();

        Polygon shape3 = new Polygon();
        shape3.addPoint(rect.x, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 10, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 1,  rect.y + rect.height / 2);
        shape3.addPoint(rect.x + rect.width - 10, rect.y);
        shape3.addPoint(rect.x, rect.y);

        g.setColor(Color.LIGHT_GRAY);
        g.drawPolygon(shape3);

    }

}

编辑:

修改代码如下。

我希望默认背景为白色所以当第一次背景为白色时创建标签时。现在屏幕上显示了几个标签。点击事件我改变背景颜色,例如我调用setbackground(Color.red),以便更新所点击标签的颜色。

现在当我滚动屏幕时,会调用repaint()方法并重新绘制所有自定义标签,并将所有标签的背景更改为红色。

编辑2: 添加标签的代码:

for (int i = 0; i < noOfLabels; i++)
    {
        String labelkey = "Label" +i;
        CustomLabel label = new CustomLabel();
        label.addMouseListener(this);
        myLayeredPane.add(label, new Integer(3));
    }

自定义标签代码:

public class CustomLabel extends JLabel
{
private Color background = Color.WHITE;

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    Rectangle rect = g.getClipBounds();

    Polygon shape3 = new Polygon();
    shape3.addPoint(rect.x, rect.y + rect.height - 1);
    shape3.addPoint(rect.x + rect.width - 10, rect.y + rect.height - 1);
    shape3.addPoint(rect.x + rect.width - 1, rect.y + rect.height / 2);
    shape3.addPoint(rect.x + rect.width - 10, rect.y);
    shape3.addPoint(rect.x, rect.y);

    g.setColor(background);
    g.fillPolygon(shape3);

    g.setColor(Color.LIGHT_GRAY);
    g.drawPolygon(shape3);
}

@Override
public void setBackground(Color arg0)
{
    background = arg0;
    System.out.println("Setting bg color to : "+background);
}

}

3 个答案:

答案 0 :(得分:1)

您可以尝试覆盖setBackground()的下一个技巧,并使用自定义颜色填充您的形状:

public class CustomLabel extends JLabel {

    private Color background = Color.RED;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect = g.getClipBounds();

        Polygon shape3 = new Polygon();
        shape3.addPoint(rect.x, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 10, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 1, rect.y + rect.height / 2);
        shape3.addPoint(rect.x + rect.width - 10, rect.y);
        shape3.addPoint(rect.x, rect.y);

        g.setColor(Color.LIGHT_GRAY);
        g.drawPolygon(shape3);
        g.setColor(background);
        g.fillPolygon(shape3);
    }

    @Override
    public void setBackground(Color arg0) {
        background = arg0;
    }
}

编辑:这是我的例子,有4个标签,一切正常:

public class CustomLabel extends JLabel {

    static Random rand = new Random();

    public static void main(String args[]) {
        JLayeredPane p = new JLayeredPane();
        p.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        p.setPreferredSize(new Dimension(200, 100));
        MouseAdapter adapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                super.mouseClicked(arg0);
                float r = rand.nextFloat();
                float g = rand.nextFloat();
                float b = rand.nextFloat();
                Color randomColor = new Color(r, g, b);
                arg0.getComponent().setBackground(randomColor);
                arg0.getComponent().repaint();
            }
        };
        for (int i = 0; i < 4; i++)
        {
            String labelkey = "Label" +i;
            CustomLabel label = new CustomLabel();
            label.setText(labelkey);
            label.setBounds(10+i*30,10,30,30);
            label.addMouseListener(adapter);
            p.add(label);
        }

        JFrame f = new JFrame();
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    public CustomLabel(){
    }

    private Color background = Color.white;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Rectangle rect = g.getClipBounds();

        Polygon shape3 = new Polygon();
        shape3.addPoint(rect.x, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 10, rect.y + rect.height - 1);
        shape3.addPoint(rect.x + rect.width - 1, rect.y + rect.height / 2);
        shape3.addPoint(rect.x + rect.width - 10, rect.y);
        shape3.addPoint(rect.x, rect.y);

        g.setColor(Color.LIGHT_GRAY);
        g.drawPolygon(shape3);
        g.setColor(background);
        g.fillPolygon(shape3);
    }

    @Override
    public void setBackground(Color arg0) {
        background = arg0;
    }

}

答案 1 :(得分:0)

查看Playing With Shapes了解绘制形状的其他一些想法,而不是扩展JLabel并进行自定义绘制。

形状可以重复使用,可以与任何可以显示Icon的组件一起使用。

答案 2 :(得分:-1)

可能是您的super.paintComponent(g); 尝试删除它,您的父母仍将绘制形状。