java - 如果文本不适合 JLabel,我想减小字体大小

标签 java swing fonts jlabel

关于这个问题的帖子很多,但是我看不懂里面的人给出的答案。 就像在这篇文章中:“How to change the size of the font of a JLabel to take the maximum size”答案将字体大小转换为 14!但这是静态的,在其他答案中更进一步;他们的整个输出屏幕似乎增加了。

我在名为“lnum”的 JLabel 中显示某些数字,它最多可以显示 3 位数字,但之后它显示为“4...”我希望如果数字能够适合标签,它应该不改变它的字体大小,但如果数字是 4 位数字,它应该以适合的方式减小字体大小。注意:我不希望 jLabel 的尺寸发生变化。我只想更改其中的文本。

编辑: 这是我试过的代码

String text = lnum.getText();        
System.out.println("String Text = "+text);//DEBUG
Font originalFont = (Font)lnum.getClientProperty("originalfont"); // Get the original Font from client properties            
 if (originalFont == null) { // First time we call it: add it
        originalFont = lnum.getFont();
        lnum.putClientProperty("originalfont", originalFont);
    }
 int stringWidth = lnum.getFontMetrics(originalFont).stringWidth(text);       

 int componentWidth = lnum.getWidth();
 stringWidth = stringWidth + 25; //DEBUG TRY
 if (stringWidth > componentWidth) { // Resize only if needed
        // Find out how much the font can shrink in width.
        double widthRatio = (double)componentWidth / (double)stringWidth;

        int newFontSize = (int)Math.floor(originalFont.getSize() * widthRatio); // Keep the minimum size

        // Set the label's font size to the newly determined size.
        lnum.setFont(new Font(originalFont.getName(), originalFont.getStyle(), newFontSize));
    }else{
        lnum.setFont(originalFont); // Text fits, do not change font size
        System.out.println("I didnt change it hahaha");//DEBUG 
 }
    lnum.setText(text);

我有一个问题,很多时候它不起作用,比如如果文本是“-28885”,它会显示“-28 ...”。

字符串宽度 = 字符串宽度 + 25;//调试尝试

我必须添加此代码以增加它获得的长度。这是我添加的代码,只是暂时解决了这个问题。我想要一个永久的解决方案。

最佳答案

改编自an answer on the question you referred to :

void setTextFit(JLabel label, String text) {
    Font originalFont = (Font)label.getClientProperty("originalfont"); // Get the original Font from client properties
    if (originalFont == null) { // First time we call it: add it
        originalFont = label.getFont();
        label.putClientProperty("originalfont", originalFont);
    }

    int stringWidth = label.getFontMetrics(originalFont).stringWidth(text);
    int componentWidth = label.getWidth();

    if (stringWidth > componentWidth) { // Resize only if needed
        // Find out how much the font can shrink in width.
        double widthRatio = (double)componentWidth / (double)stringWidth;

        int newFontSize = (int)Math.floor(originalFont.getSize() * widthRatio); // Keep the minimum size

        // Set the label's font size to the newly determined size.
        label.setFont(new Font(originalFont.getName(), originalFont.getStyle(), newFontSize));
    } else
        label.setFont(originalFont); // Text fits, do not change font size

    label.setText(text);
}

当您要显示一个合适的数字时,您应该将字体重置为原来的字体(参见 else 部分)。

编辑:如果您不能/不想保留对原始字体的引用,您可以将其保存为“客户端属性”(参见第一行)。

关于java - 如果文本不适合 JLabel,我想减小字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194699/

相关文章:

游戏循环和按钮的 Java 图形问题

html - -webkit 文本大小调整 : 100% doesn't work on iphone

css - 加粗内容需要更长的时间来加载

java - "d"符号在适用于某些对象的方法列表中代表什么?

java - 抽象类实例化内部类

java - 如何退出 Java 中的 do while 循环?

.net - unicode 实现 : many fonts, 还是一种大字体?

java - Quartz Java 恢复作业会执行多次

java - JavaBeans 对 OCA 方法命名约定的误解

java - JCheckBoxTree初始状态问题