【问题标题】:Show JLabel on a graphic drawn with paintComponent在使用paintComponent 绘制的图形上显示JLabel
【发布时间】:2015-09-21 16:34:35
【问题描述】:

我有一个扩展 JLabel 的课程。这个 JLabel 有一个特殊的形状,我在方法 paintComponent 中绘制它。我想在 jLabel 的中心显示一个文本,但没有显示这个文本。谁能帮帮我。

我的简单 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);
    }

}

【问题讨论】:

    标签: java swing jlabel paintcomponent


    【解决方案1】:

    我想在 jLabel 的中心显示一个文本,但是这个文本没有显示出来。

    super.paintComponent() 将绘制文本,但随后您的自定义绘制将在文本顶部绘制多边形。

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

    您可以使用以下方法使文本在标签上居中:

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

    这是一个创建矩形图标的简单示例:

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

    我会让你修改多边形的代码。

    您还可以查看Playing With Shapes,了解一些创建不同形状图标的有趣方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2016-10-13
      相关资源
      最近更新 更多