在JLabel的开头放置3个点 [英] Putting 3 dots at the beginning of JLabel

查看:84
本文介绍了在JLabel的开头放置3个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当JLabel中的文本太长时,在文本末尾会有可见的3个点.是否可以将它们放在开头?

When the text in JLabel is too long there are visible 3 dots at the end of text. Is it possible to put them at the beginning?

推荐答案

您可以考虑使用FontMetrics.类,以查看当前字体下的文本长度.

You may consider using FontMetrics. class to see the length of your text under the current font.

_________________________________
|
| This is some really long text that I want to fit in the small label
|________________________________

       ^^^ YOUR LABEL ^^^  

说您想将较长的文本放入该标签中.
这是您可以做的(这只是一个疯狂的猜测,我正在动态进行中)

Say you want to fit that long text into that label.
Here is what you can do (and this is just a wild guess and I am making this on the fly)

  1. String中的三个点...开始.
  2. 开始向其添加附加字符.
  3. 获取您的JLabel的宽度.
  4. 使用FontMetrics来测量文本的长度(以像素为单位),以添加更多字符
  5. 只要文本的像素长度小于您的JLabel
  6. 的宽度,请继续添加更多字符
  7. 一旦它变得大于JLabel的宽度,就退出循环.
  8. 将此新形成的文本设置为您的JLabel
  9. 的文本
  1. Start with your three dots ... in a String.
  2. Start adding appending characters to it, one by one.
  3. Get the width of your JLabel.
  4. Use FontMetrics to measure the length of your text , in pixels, as you append more characters
  5. Keep adding more characters as long as the pixel length of the text is less than the width of your JLabel
  6. Once it becomes greater than the width of the JLabel, get out of the loop.
  7. Set this newly formed text as the text of your JLabel

您应该这样结束:

_________________________________
|
| ...This is some really long tex
|________________________________

       ^^^ YOUR LABEL ^^^  

这是开始使用FontMetrics的简单方法.避免在那里争吵.只需执行接受的答案即可: Java:获取FontMetrics实例的更友好方式

Here is an easy way to get started with FontMetrics. Avoid the bickering there. Just do what the accepted answer says: Java: Friendlier way to get an instance of FontMetrics

SSCCE 符合OP的实际需求,而不是我的解释

package stack;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class BackwardsDots extends JFrame{

    JLabel label = new JLabel(){
                        @Override
                        public Dimension getPreferredSize(){
                            return new Dimension(200,100);
                        }
                    };
    String text = "This is a design requirement and not my whim";
    FontMetrics fm;
    Font theFontBeingUsed;
//--------------------------------------------------------------------------------  
    public BackwardsDots(){
        getContentPane().add(label);
        pack();

        theFontBeingUsed = new Font("Ubuntu",Font.BOLD,14);
        fm = Toolkit.getDefaultToolkit().getFontMetrics(theFontBeingUsed);



        label.setText(trimmedStringCalculator(text));
        label.setToolTipText(text);
        label.setBorder(BorderFactory.createDashedBorder(Color.RED));
        label.addComponentListener(new ComponentListener(){

            @Override
            public void componentHidden(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentMoved(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void componentResized(ComponentEvent arg0) {
                label.setText(trimmedStringCalculator(text));
            }

            @Override
            public void componentShown(ComponentEvent arg0) {
                // TODO Auto-generated method stub

            }

        });

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
//--------------------------------------------------------------------------------
    private String trimmedStringCalculator(String inputText){
        String ellipses = "...";
        String textToBeDisplayed = "";

        int widthOfJLabel = label.getWidth();

        for(int i = text.length()-1; i >= 0; i--){
            if(fm.stringWidth(ellipses + textToBeDisplayed) <= widthOfJLabel){
                textToBeDisplayed = text.charAt(i) + textToBeDisplayed;
            }
        }

        String finalText;
        if(textToBeDisplayed.equals(inputText)){
            finalText = inputText;
        }else{
            finalText = ellipses.concat(textToBeDisplayed);
        }

        return finalText;
    }
//--------------------------------------------------------------------------------  
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new BackwardsDots();
            }
        });
    }
}

输出

这篇关于在JLabel的开头放置3个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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