在用paintComponent绘制的图形上显示JLabel [英] Show JLabel on a graphic drawn with paintComponent

查看:80
本文介绍了在用paintComponent绘制的图形上显示JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展 JLabel 的类.这个JLabel具有奇异的形状,我在 paintComponent 方法中绘制它.我想在jLabel的中心显示一个文本,但是未显示该文本.谁能帮我.

I've a class extending JLabel. This JLabel has a particolar shape and I draw that in the method paintComponent. I want to show a text in the center of the jLabel but this text is not shown. Could anyone help me.

以下是我的简单HLabel类:

My simple HLabel class in the following:

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

}

推荐答案

我想在jLabel的中心显示一个文本,但此文本没有显示.

I want to show a text in the center of the jLabel but this text is not shown.

super.paintComponent()将绘制文本,但是您的自定义绘制将在文本上方绘制多边形.

The super.paintComponent() will paint the text, but then your custom painting will paint the polygon over top of the text.

不要覆盖JLabel.相反,您可以创建一个 PolygonIcon .然后将Icon和文本添加到JLabel.

Don't override the JLabel. Instead you can create a PolygonIcon. Then you add the Icon and text to the 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.

这篇关于在用paintComponent绘制的图形上显示JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆