簡體   English   中英

獲取 jlabel 中文本的邊界

[英]Getting the bounds of the text inside a jlabel

我想從 JLabel 中的文本繪制一個箭頭到 JLabel 之外的點。 要在適當的位置開始箭頭,我需要 JLabel 中實際文本的邊界。 我在下面的回答將展示如何獲得這些界限。

Swing API 已經提供了 API 來執行這個操作 - SwingUtilities#layoutCompoundLabel

protected TextBounds calculateTextBounds(JLabel label) {

    Rectangle paintIconR = new Rectangle();
    Rectangle paintTextR = new Rectangle();

    Dimension size = label.getSize();
    Insets insets = label.getInsets(null);
    String text = label.getText();
    Icon icon = (label.isEnabled()) ? label.getIcon()
                    : label.getDisabledIcon();
    Rectangle paintViewR = new Rectangle();
    paintViewR.x = insets.left;
    paintViewR.y = insets.top;
    paintViewR.width = size.width - (insets.left + insets.right);
    paintViewR.height = size.height - (insets.top + insets.bottom);
    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String result = SwingUtilities.layoutCompoundLabel(
                    (JComponent) label,
                    label.getFontMetrics(label.getFont()),
                    text,
                    icon,
                    label.getVerticalAlignment(),
                    label.getHorizontalAlignment(),
                    label.getVerticalTextPosition(),
                    label.getHorizontalTextPosition(),
                    paintViewR,
                    paintIconR,
                    paintTextR,
                    label.getIconTextGap());

    TextBounds bounds = new TextBounds(paintViewR, paintTextR, paintIconR);

    return bounds;
}

基本上這將提供“視圖”邊界(基本上是可以繪制 label 的區域)、“文本”邊界和“圖標”邊界,我只是將它們包裝在一個易於使用的 class 中

那么為什么要使用這種方法呢?

  • 這實際上與外觀代表用於布局 label 的工作流程相同
  • 它不需要您創建自定義 class,這意味着它可以應用於JLabel的任何實例

標簽文本邊界

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TextBounds {

        private Rectangle paintBounds;
        private Rectangle textBounds;
        private Rectangle iconBounds;

        public TextBounds(Rectangle paintBounds, Rectangle textBounds, Rectangle iconBounds) {
            this.paintBounds = paintBounds;
            this.textBounds = textBounds;
            this.iconBounds = iconBounds;
        }

        public Rectangle getPaintBounds() {
            return paintBounds;
        }

        public Rectangle getTextBounds() {
            return textBounds;
        }

        public Rectangle getIconBounds() {
            return iconBounds;
        }

    }

    public class TestPane extends JPanel {

        private JLabel label = new JLabel("Hello");

        public TestPane() {
            setLayout(new BorderLayout());
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            add(label);
        }

        protected TextBounds calculateTextBounds(JLabel label) {

            Rectangle paintIconR = new Rectangle();
            Rectangle paintTextR = new Rectangle();

            Dimension size = label.getSize();
            Insets insets = label.getInsets(null);
            String text = label.getText();
            Icon icon = (label.isEnabled()) ? label.getIcon()
                            : label.getDisabledIcon();
            Rectangle paintViewR = new Rectangle();
            paintViewR.x = insets.left;
            paintViewR.y = insets.top;
            paintViewR.width = size.width - (insets.left + insets.right);
            paintViewR.height = size.height - (insets.top + insets.bottom);
            paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
            paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

            String result = SwingUtilities.layoutCompoundLabel(
                            (JComponent) label,
                            label.getFontMetrics(label.getFont()),
                            text,
                            icon,
                            label.getVerticalAlignment(),
                            label.getHorizontalAlignment(),
                            label.getVerticalTextPosition(),
                            label.getHorizontalTextPosition(),
                            paintViewR,
                            paintIconR,
                            paintTextR,
                            label.getIconTextGap());

            TextBounds bounds = new TextBounds(paintViewR, paintTextR, paintIconR);

            System.out.println(result);
            System.out.println("view " + paintViewR);
            System.out.println("paint " + paintIconR);
            System.out.println("text " + paintTextR);

            return bounds;
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            TextBounds bounds = calculateTextBounds(label);
            g2d.setColor(Color.RED);
            g2d.draw(bounds.getPaintBounds());
            g2d.setColor(Color.GREEN);
            g2d.draw(bounds.getTextBounds());
            g2d.setColor(Color.BLUE);
            g2d.draw(bounds.getIconBounds());
            g2d.dispose();
        }

    }

}

其他解決方案的潛在優勢是不會在每次調用時創建三個新的 Rectangle 對象。 如果在 MouseMoved 事件偵聽器中調用 getTextBounds,這可能會成為問題。 以復雜性為代價,最終的 Rectangle 可以與 JLabel 的寬度和高度一起緩存。

Rectangle只需要創建一次,它們被傳遞到 API 並直接應用它們的值,因此您不必每次都創建新對象。

您也不需要使用專門的組件,可以使用現有 label 上的工作流

您可以使用SwingUtilities.layoutCompoundLabel

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

public class LabelLayout extends JLabel
{
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics grid = g.create();
        grid.setColor( Color.ORANGE );

        Rectangle viewR = new Rectangle();
        viewR.width = getSize().width;
        viewR.height = getSize().height;
        Rectangle iconR = new Rectangle();
        Rectangle textR = new Rectangle();

        String clippedText = SwingUtilities.layoutCompoundLabel
        (
            this,
            grid.getFontMetrics(),
            getText(),
            getIcon(),
            getVerticalAlignment(),
            getHorizontalAlignment(),
            getVerticalTextPosition(),
            getHorizontalTextPosition(),
            viewR,
            iconR,
            textR,
            getIconTextGap()
        );

        int gridSize = 10;
        int start = iconR.x;
        int end = iconR.x + iconR.width;

        System.out.println("Text bounds: " + textR );
        System.out.println("Icon bounds: " + iconR );

        for (int i = start; i < end; i += gridSize)
        {
            grid.drawLine(i, iconR.y, i, iconR.y + iconR.height);
        }

        grid.dispose();
    }

    private static void createAndShowGUI()
    {
        LabelLayout label = new LabelLayout();
        label.setBorder( new LineBorder(Color.RED) );
        label.setText( "Some Text" );
        label.setIcon( new ImageIcon( "DukeWaveRed.gif" ) );
        label.setVerticalAlignment( JLabel.CENTER );
        label.setHorizontalAlignment( JLabel.CENTER );
//      label.setVerticalTextPosition( JLabel.BOTTOM );
        label.setVerticalTextPosition( JLabel.TOP );
        label.setHorizontalTextPosition( JLabel.CENTER );

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( label );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setSize(300, 200);
        frame.setVisible( true );
    }

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

此示例在 label 中的圖標上方繪制了額外的線條。

要獲取 JLabel 中文本的邊界,我們需要可訪問的上下文,一個受保護的字段。 所以我們擴展了 JLabel。 可訪問的上下文只知道文本是 HTML,所以我在構造函數中添加了“<html>”; 更通用的版本將首先檢查文本是否已經以該字符串開頭。

   class FieldLabel extends JLabel {
        FieldLabel(String text) {
            super("<html>"+text);
        }
        Rectangle getTextBounds() {
            JLabel.AccessibleJLabel acclab 
                = (JLabel.AccessibleJLabel) getAccessibleContext();
            if (acclab.getCharCount() <= 0)
                return null;
            Rectangle r0 = acclab.getCharacterBounds(0);
            Rectangle rn = acclab
                .getCharacterBounds(acclab.getCharCount()-1);

            return new Rectangle(r0.x, r0.y, 
                    rn.x+rn.width-r0.x, Math.max(r0.height, rn.height));
        }
    }

其他解決方案的潛在優勢是不會在每次調用時創建三個新的 Rectangle 對象。 如果在 MouseMoved 事件偵聽器中調用 getTextBounds,這可能會成為問題。 以復雜性為代價,最終的 Rectangle 可以與 JLabel 的寬度和高度一起緩存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM