即使动态更改字体大小后,JLabel 也不显示所有字符

发布于 2024-09-02 12:04:34 字数 2209 浏览 0 评论 0原文

我试图将一个经常变化的句子放入几个 jlabel 中。我的 3 个 jlabel 的宽度始终保持不变。我正在做的是更改字体大小,以便所有字符都可以适合而不超出标签的显示范围。我所做的就是在句子改变时调用下面的代码片段。

这是我的代码

    String sentence = "Some long sentence";
    int SentenceLength = sentence.length();
    int FontSize = 0;
    // sum of widths of the three labels
    int TotalLblLength=lbl_0ValueInWords.getWidth()+lbl_1ValueInWords.getWidth()+lbl_1ValueInWords.getWidth();

    /*decide the font size so that all the characters can be displayed 
     with out exceeding the display renge(horizontal) of the 3 labels 
     Inconsolata -> monopace font
     font size == width of the font*2 (something I observed, not sure 
     if this is true always)  */
    FontSize=(TotalLblLength/SentenceLength)*2;          
    // max font size is 20 - based on label height
    FontSize=(FontSize>20)?20:FontSize; 

    lbl_0ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_1ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_2ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));

    int CharCount_lbl0 = width_lbl0 / (FontSize / 2);
    int CharCount_lbl1 = width_lbl1 / (FontSize / 2);
    int CharsCount_lbl2 = width_lbl2 / (FontSize / 2);

    /*Set texts of each label
     if sentence has more than the number of characters that can fit in the
     1st label, excessive characters are moved to the 2nd label. same goes 
     for the 2nd and 3rd labels*/
    if (SentenceLength > CharCount_lbl0) {
        lbl_0ValueInWords.setText(sentence.substring(0, CharCount_lbl0));
        if (SentenceLength > CharCount_lbl0 + CharCount_lbl1) {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, CharCount_lbl0 + CharCount_lbl1));
            lbl_2ValueInWords.setText(sentence.substring(CharCount_lbl0 + CharCount_lbl1, SentenceLength));
        } else {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, SentenceLength));
        }
    } else {

        lbl_0ValueInWords.setText(sentence);
    }

,但即使在重置字体大小后,有时最后一个字符也会超出显示范围。我已经从 jlabel 中删除了可能导致此问题的边距。对于随机长度的句子会发生这种情况。我可以通过减少用于计算的标签宽度来解决应用程序的问题(希望如此)

任何人都可以解释我的原因吗?可能是因为字体对称性存在缺陷?

I am trying to fit a sentence that changes often, in to a few jlabels. Widths of my 3 jlabels stay unchanged all the time. What I am doing is changing the font size so all the characters can fit with out non being out of the display range of the labels. What I do is call below code snippet when ever sentence is changed.

Here is my code

    String sentence = "Some long sentence";
    int SentenceLength = sentence.length();
    int FontSize = 0;
    // sum of widths of the three labels
    int TotalLblLength=lbl_0ValueInWords.getWidth()+lbl_1ValueInWords.getWidth()+lbl_1ValueInWords.getWidth();

    /*decide the font size so that all the characters can be displayed 
     with out exceeding the display renge(horizontal) of the 3 labels 
     Inconsolata -> monopace font
     font size == width of the font*2 (something I observed, not sure 
     if this is true always)  */
    FontSize=(TotalLblLength/SentenceLength)*2;          
    // max font size is 20 - based on label height
    FontSize=(FontSize>20)?20:FontSize; 

    lbl_0ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_1ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));
    lbl_2ValueInWords.setFont(new java.awt.Font("Inconsolata", 0,FontSize));

    int CharCount_lbl0 = width_lbl0 / (FontSize / 2);
    int CharCount_lbl1 = width_lbl1 / (FontSize / 2);
    int CharsCount_lbl2 = width_lbl2 / (FontSize / 2);

    /*Set texts of each label
     if sentence has more than the number of characters that can fit in the
     1st label, excessive characters are moved to the 2nd label. same goes 
     for the 2nd and 3rd labels*/
    if (SentenceLength > CharCount_lbl0) {
        lbl_0ValueInWords.setText(sentence.substring(0, CharCount_lbl0));
        if (SentenceLength > CharCount_lbl0 + CharCount_lbl1) {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, CharCount_lbl0 + CharCount_lbl1));
            lbl_2ValueInWords.setText(sentence.substring(CharCount_lbl0 + CharCount_lbl1, SentenceLength));
        } else {
            lbl_1ValueInWords.setText(sentence.substring(CharCount_lbl0, SentenceLength));
        }
    } else {

        lbl_0ValueInWords.setText(sentence);
    }

But even after resetting font size sometimes the last character goes out of the display range. I have removed margines from the jlabels that may cause this. This happens for random length sentences. I can solve the problem for the application by reducing label width used for the calculations(hopefully)

Can anyone explain me the reason? Could be because of some defect in the fonts symmetry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-09-09 12:04:34

没有字体对称之类的东西吗?

有两种类型的字体适合您正在处理的内容。等宽字体和非等宽字体。等宽字体对于您可以键入的每个字符具有相同的精确宽度。其他人则不然。

最重要的是,不同操作系统的字体呈现方式也不同。 Windows 上的某些内容在 Mac 上会长大约 10-20%,因为它们的字体间距不同。

无论您想用 JLabels 做什么,请停止。您不应该使用 3 个 JLabel 来显示 3 行文本,因为它们不合适。废弃它们并使用 JTextArea。它具有文本换行功能,您可以设置字体,并删除边距/边框/填充并使其不可编辑。您可以非常轻松地自定义它,因此它与 JLabel 没有什么区别,但它会为您节省大量工作。

为正确的工作选择正确的工具。

There is no such thing as font symmetry?

There are 2 types of fonts for what you are dealing with. Monospace fonts, and non-monospace fonts. Monospace fonts have the same exact width for every single character you can type. The others do not.

On top of that, fonts are rendered differently across different OS's. Something on windows will be around 10-20% longer on Mac because they space out the fonts differently.

Whatever it is you are trying to do with JLabels, stop. You should not be using 3 JLabels to show 3 lines of text because they dont fit. Scrap them and use a JTextArea. It has text wrap, you can set the font, and remove the margin/border/padding and make it non-editable. You can customize it very easily so it is indistinguishable from a JLabel, but it will save you a ton of work.

Pick the right tool for the right job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文