简体   繁体   English

在用paintComponent绘制的图形上显示JLabel

[英]Show JLabel on a graphic drawn with paintComponent

I've a class extending JLabel . 我有一个扩展JLabel的课程。 This JLabel has a particolar shape and I draw that in the method paintComponent . 这个JLabel具有奇异的形状,我在paintComponent方法中进行绘制。 I want to show a text in the center of the jLabel but this text is not shown. 我想在jLabel的中心显示一个文本,但是未显示该文本。 Could anyone help me. 谁能帮我。

My simple HLabel class in the following: 我的简单HLabel类如下:

private class Scudetto extends JLabel {

    private static final long serialVersionUID = 1L;

    public Scudetto(String line_point)
    {
        super(line_point, SwingUtilities.CENTER);
        this.setOpaque(true);
        this.setBackground(Color.BLACK);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension d = this.getSize();

        int[] x = { 0, d.width,      d.width, d.width / 2,             0 };
        int[] y = { 0,       0, d.height / 2,     d.height, d.height / 2 };

        g.setColor(Color.WHITE);
        g.fillPolygon(x, y, 5);

        g.setColor(Color.BLACK);

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(10, 20);
    }

}

I want to show a text in the center of the jLabel but this text is not shown. 我想在jLabel的中心显示一个文本,但是未显示该文本。

The super.paintComponent() will paint the text, but then your custom painting will paint the polygon over top of the text. super.paintComponent()将绘制文本,但是您的自定义绘制将在文本上方绘制多边形。

Don't override the JLabel. 不要重写JLabel。 Instead you can create a PolygonIcon . 相反,您可以创建PolygonIcon Then you add the Icon and text to the JLabel. 然后,将Icon和文本添加到JLabel。

You can have the text centered on the label by using: 您可以使用以下方法将文本居中放置在标签上:

JLabel label = new JLabel("your text");
label.setIcon( polygonIcon );
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

Here is a simple example of creating a rectangular Icon: 这是创建矩形图标的简单示例:

import java.awt.*;
import javax.swing.*;

public class ColorIcon implements Icon
{
    private Color color;
    private int width;
    private int height;

    public ColorIcon(Color color, int width, int height)
    {
        this.color = color;
        this.width = width;
        this.height = height;
    }

    public int getIconWidth()
    {
        return width;
    }

    public int getIconHeight()
    {
        return height;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI()
    {
        JPanel panel = new JPanel( new GridLayout(2, 2) );

        for (int i = 0; i < 4; i++)
        {
            Icon icon = new ColorIcon(Color.RED, 50, 50);
            JLabel label = new JLabel( icon );
            label.setText("" + i);
            label.setHorizontalTextPosition(JLabel.CENTER);
            label.setVerticalTextPosition(JLabel.CENTER);
            panel.add(label);
        }

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.setSize(200, 200);
        f.setLocationRelativeTo( null );
        f.setVisible(true);
    }
}

I'll let you modify the code for a polygon. 我将让您修改多边形的代码。

You can also check out Playing With Shapes for some fun ways to create an Icon of different shapes. 您还可以查看“ 使用形状玩”以获得一些有趣的方式来创建不同形状的图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM