0

我通过覆盖 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);
}

}
4

3 回答 3

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;
    }

}
于 2013-11-20T14:16:21.593 回答
0

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

这些形状将是可重复使用的,并且可以与任何可以显示Icon.

于 2017-02-25T16:08:45.610 回答
-1

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

于 2013-11-20T13:57:23.010 回答