English 中文(简体)
根据特定Width计算出的JLabel文本
原标题:Calculate Height of JLabel text based on specific Width

从事Marquee JPanel 的工作,并在JPanel 内部利用JLabel在JPanel 内部浏览JLabel的Y。

问题在于,需要一名特别的JLabel的妻子,他并不重要,因为它将在JLabel的Y族人“与JLabel”之前将案文推移。 意指JLabel最后行文的是JPanel 。

因此,如何计算根据Width的具体价值可以持有某种文本的重量。

eg. If Width of JLabel is 50 than the Height should be that value that can fit the text within if text is "Bla bla bla some text over ..." and font is "Serif", Font.ITALIC, 20

最佳回答

Despite that this is answered, I thought I d offer an alternative approach. This alternate boils down to put the text it in an HTML formatted label . The advantage of this approach is that line breaks are normally handled automatically, without having to worry about where to break each line. E.G.

import javax.swing.*;

class HeightOfFixedWidthText {

    public static void show(String s) {
        JLabel l = new JLabel(s);
        l.setSize(l.getPreferredSize());
        JOptionPane.showMessageDialog(null, l);
        System.out.println(l.getSize());
    }

    public static void main(String[] srgs) {
        String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        String html1 = "<html><body " +
            "style= font-family: Serif; font-style: italic; font-size: 20px; padding: 0px; margin: 0px;" +
            " width: ";
        String html2 = "px >";
        String html3 = "</body></html>";

        show(html1+"200"+html2+s+html3);
        show(html1+"300"+html2+s+html3);
    }
}

Output

java.awt.Dimension[width=260,height=884]
java.awt.Dimension[width=390,height=544]

Caveat

  1. There is probably some (more) padding in the body of that HTML that needs to be removed in order to get the smallest size needed to display the the text. Left as an exercise for the user.
  2. Swing s HTML formatting is notorious for getting the baseline of text wrong. This is likely to count if the text content includes things like sub-scripts which drop below the usual baseline of the text. It seems as though that won t be a problem in this use-case.
  3. I only just noticed that though the width is specified as 200 then 300, the result comes back as 260 & 390. I could understand if that difference was constant between the two (extra padding to remove), but am quite mystified as to why it is different for each label.
问题回答

您不妨查看FontMetrics,该类别有根据体大小获得体属性的方法,如高、宽、cent等。 鉴于这一信息,你应当能够从宽度上消除某个特定体/大小的高度,反之亦然。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签