Java 如何更改 JLabel 的字体大小以取最大大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2715118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:01:27  来源:igfitidea点击:

How to change the size of the font of a JLabel to take the maximum size

javafontssizecontainersjlabel

提问by g123k

I have a JLabelin a Container. The defaut size of the font is very small. I would like that the text of the JLabelto take the maximum size.

我有一个JLabel在容器中。字体的默认大小非常小。我希望文本JLabel采用最大尺寸。

How can I do that?

我怎样才能做到这一点?

采纳答案by coobird

Not the most pretty code, but the following will pick an appropriate font size for a JLabelcalled labelsuch that the text inside will fit the interior as much as possible without overflowing the label:

不是最漂亮的代码,但以下代码将为JLabel调用选择合适的字体大小,以便label里面的文本尽可能地适合内部,而不会溢出标签:

Font labelFont = label.getFont();
String labelText = label.getText();

int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
int componentWidth = label.getWidth();

// Find out how much the font can grow in width.
double widthRatio = (double)componentWidth / (double)stringWidth;

int newFontSize = (int)(labelFont.getSize() * widthRatio);
int componentHeight = label.getHeight();

// Pick a new font size so it will not be larger than the height of label.
int fontSizeToUse = Math.min(newFontSize, componentHeight);

// Set the label's font size to the newly determined size.
label.setFont(new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse));

Basically, the code looks at how much space the text in the JLabeltakes up by using the FontMetricsobject, and then uses that information to determine the largest font size that can be used without overflowing the text from the JLabel.

基本上,代码JLabel通过使用FontMetrics对象来查看文本中的文本占用了多少空间,然后使用该信息来确定可以使用的最大字体大小,而不会从JLabel.

The above code can be inserted into perhaps the paintmethod of the JFramewhich holds the JLabel, or some method which will be invoked when the font size needs to be changed.

上面的代码可能插入到持有的paint方法中,或者在需要改变字体大小时调用的一些方法。JFrameJLabel

The following is an screenshot of the above code in action:

下面是上面代码的截图:

alt text
(source: coobird.net)

替代文字
(来源:coobird.net

回答by Asaf David

label = new JLabel("A label");
label.setFont(new Font("Serif", Font.PLAIN, 14));

taken from How to Use HTML in Swing Components

摘自《如何在 Swing 组件中使用 HTML》

回答by Ehsan Jelodar

 JLabel textLabel = new JLabel("<html><span style='font-size:20px'>"+Text+"</span></html>");

回答by Bekim Fetaji

Source Code for Label - How to change Color and Font (in Netbeans)

标签的源代码 - 如何更改颜色和字体(在 Netbeans 中)

jLabel1.setFont(new Font("Serif", Font.BOLD, 12));


jLabel1.setForeground(Color.GREEN);

回答by Warren K

Just wanted to point out that the accepted answer has a couple of limitations (which I discovered when I tried to use it)

只是想指出接受的答案有几个限制(我在尝试使用它时发现)

  1. As written, it actually keeps recalculating the font size based on a ratio of the previousfont size... thus after just a couple of calls it has rendered the font size as much too large. (eg Start with 12 point as your DESIGNED Font, expand the label by just 1 pixel, and the published code will calculate the Font size as 12 * (say) 1.2 (ratio of field space to text) = 14.4 or 14 point font. 1 more Pixel and call and you are at 16 point !).
  1. 正如所写的那样,它实际上会根据先前字体大小的比率重新计算字体大小……因此,在几次调用之后,它已经使字体大小变得太大了。(例如,从 12 磅作为您的设计字体开始,将标签扩展仅 1 像素,发布的代码将计算字体大小为 12 *(例如)1.2(字段空间与文本的比率)= 14.4 或 14 磅字体。再打 1 个 Pixel 并打电话,你就 16 点了!)。

It is thus not suitable (without adaptation) for use in a repeated-call setting (eg a ComponentResizedListener, or a custom/modified LayoutManager).

因此,它不适合(未经调整)用于重复呼叫设置(例如 aComponentResizedListener或 custom/modified LayoutManager)。

The listed code effectively assumes a starting size of 10 pt but refers to the current font size and is thus suitable for calling once (to set the size of the font when the label is created). It would work better in a multi-call environment if it did int newFontSize = (int) (widthRatio * 10);rather than int newFontSize = (int)(labelFont.getSize() * widthRatio);

列出的代码有效地假定起始大小为 10 pt,但指的是当前字体大小,因此适合调用一次(在创建标签时设置字体大小)。如果这样做,它会在多呼叫环境中更好地工作,int newFontSize = (int) (widthRatio * 10);而不是int newFontSize = (int)(labelFont.getSize() * widthRatio);

  1. Because it uses new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse))to generate the new font, there is no support for Bolding, Italic or Color etc from the original font in the updated font. It would be more flexible if it made use of labelFont.deriveFontinstead.

  2. The solution does not provide support for HTML label Text. (I know that was probably not ever an intended outcome of the answer code offered, but as I had an HTML-text JLabelon my JPanelI formally discovered the limitation. The FontMetrics.stringWidth()calculates the text length as inclusiveof the width of the html tags - ie as simply more text)

  1. 因为它用于new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse))生成新字体,所以更新字体中不支持原始字体的粗体、斜体或颜色等。如果改用它会更灵活labelFont.deriveFont

  2. 该解决方案不提供对 HTML 标签文本的支持。(我知道这可能不是所提供的答案代码的预期结果,但由于我有一个 HTML 文本JLabel,我JPanel正式发现了限制。FontMetrics.stringWidth()计算文本长度为包括html 标签的宽度 - 即作为只是更多的文字)

I recommend looking at the answer to this SO questionwhere trashgod's answer points to a number of different answers (including this one) to an almost identical question. On that page I will provide an additional answer that speeds up one of the other answers by a factor of 30-100.

我建议查看这个 SO 问题的答案,其中垃圾神的答案指向几乎相同问题的许多不同答案(包括这个)。在该页面上,我将提供一个额外的答案,将其他答案之一加快 30-100 倍。

回答by ataullahtoffar

JLabel label = new JLabel("Hello World");
label.setFont(new Font("Calibri", Font.BOLD, 20));