对于Custom JLabel(shape changed),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),以便更新点击标签的颜色。

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

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

您可以尝试覆盖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;
    }
}

查看"玩形状",了解绘制形状的其他想法,而不是扩展JLabel和进行自定义绘制。

这些形状将是可重复使用的,并且可以与任何可以显示Icon的组件一起使用。

可能是您的super.paintComponent(g);试着去掉它,你的父母仍然会画出这个形状。

最新更新