Java 对于自定义JLabel(shape changed),setbackground方法绘制矩形,而不是新形状

Java 对于自定义JLabel(shape changed),setbackground方法绘制矩形,而不是新形状,java,swing,jlabel,paintcomponent,setbackground,Java,Swing,Jlabel,Paintcomponent,Setbackground,我通过重写paintComponent()方法创建了一个自定义标签,如下所示。 但是当我在这个组件上调用setBackground()方法时。它绘制整个矩形。 我希望它只绘制自定义形状。请帮助 自定义标签代码: public class CustomLabel extends JLabel { public void paintComponent(Graphics g) { super.paintComponent(g); Rectangle r

我通过重写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));
    }

它可能是您的
super.paintComponent(g)

尝试移除该形状,您的家长仍将绘制该形状

您可以尝试下一个技巧,覆盖
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;
    }

}
公共类CustomLabel扩展了JLabel{
静态随机兰德=新随机();
公共静态void main(字符串参数[]){
JLayeredPane p=新的JLayeredPane();
p、 setboorder(BorderFactory.createLineBorder(Color.BLACK));
p、 setPreferredSize(新尺寸(200100));
MouseAdapter适配器=新的MouseAdapter(){
@凌驾
公共无效鼠标单击(鼠标事件arg0){
超级鼠标点击(arg0);
float r=rand.nextFloat();
float g=rand.nextFloat();
float b=rand.nextFloat();
颜色随机性颜色=新颜色(r、g、b);
arg0.getComponent().setBackground(随机颜色);
arg0.getComponent().repaint();
}
};
对于(int i=0;i<4;i++)
{
字符串labelkey=“Label”+i;
CustomLabel=新的CustomLabel();
label.setText(labelkey);
标签.立根(10+i*30,10,30,30);
标签:addMouseListener(适配器);
p、 添加(标签);
}
JFrame f=新的JFrame();
f、 加(p);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 包装();
f、 setVisible(真);
}
公共自定义标签(){
}
私有颜色背景=Color.white;
公共组件(图形g){
超级组件(g);
矩形rect=g.getClipBounds();
多边形形状3=新多边形();
形状3.addPoint(rect.x,rect.y+rect.height-1);
形状3.addPoint(矩形x+矩形宽度-10,矩形y+矩形高度-1);
形状3.addPoint(rect.x+rect.width-1,rect.y+rect.height/2);
形状3.添加点(矩形x+矩形宽度-10,矩形y);
形状3.添加点(矩形x,矩形y);
g、 setColor(颜色:浅灰色);
g、 绘制多边形(形状3);
g、 设置颜色(背景);
g、 填充多边形(形状3);
}
@凌驾
公共空间背景(颜色arg0){
背景=arg0;
}
}
查看其他一些绘制形状的想法,而不是扩展JLabel和进行自定义绘制


这些形状可以重复使用,并且可以与任何可以显示
图标的组件一起使用

super.paintComponent(g);必须be@alex2410我不同意你的说法。提出的问题表明,他只想绘制自定义形状。当他使用super.paintcomponent(g)时,它将绘制文本和其他背景。你建议一种不好的做法,请阅读有关自定义绘画的文档。非常感谢。但现在的问题是,无论何时调用repaint()方法。始终使用设置的新背景色绘制面板。如果添加了自定义面板列表,则所有面板都将使用新的bg颜色进行喷漆:(举个例子,我听不懂你的意思。编辑了我上面的问题。你如何添加和创建标签?对我来说一切都很好。也许你对所有标签都有一个引用?在上面的问题中添加了用于创建标签的代码。每个标签都有一个新的引用。)。
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;
    }

}