【问题标题】:For a Custom JLabel (shape changed) setbackground method paints the rectangle and not the new shape对于自定义 JLabel(形状已更改),setbackground 方法绘制矩形而不是新形状
【发布时间】:2013-11-20 13:49:27
【问题描述】:

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

}

【问题讨论】:

    标签: java swing jlabel paintcomponent setbackground


    【解决方案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;
        }
    
    }
    

    【讨论】:

    • 非常感谢。但现在的问题是,无论何时调用 repaint() 方法。面板总是使用设置的新背景颜色绘制。如果添加了自定义面板列表,则所有面板都将使用新的 bg 颜色进行绘制。 :(
    • 举个例子,我听不懂你的意思。
    • 在上面编辑了我的问题。
    • 如何添加和创建标签?对我来说一切都很好。也许您对所有标签都有一个引用?
    • 添加了用于在上述问题中创建标签的代码。每个标签都有一个新的引用。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • super.paintComponent(g);必须是
      • @alex2410 我不同意你的说法。提出的问题表明他只想绘制自定义形状。当他使用 super.paintCompoent(g) 时,它将绘制文本和其他背景。
      • 您建议一个不好的做法,阅读有关自定义绘画的文档。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      相关资源
      最近更新 更多