如果文本不适合JLabel,我想减小字体大小 [英] I want to decrease the font size if text doesn't fit in JLabel
问题描述
喜欢在这篇文章中:如何更改JLabel的字体大小以获取最大大小答案将字体大小转换为14!但这是静态的,还有其他的答案。他们的整个输出屏幕似乎增加。
我显示一个名为lnum的JLabel中的某些数字,它可以显示数字高达3位数字,但之后显示为4 ...我想,如果数字能够适应标签,它不应该改变它的字体大小,但如果像一个数字是4位数字,它应该减少字体大小,以适合的方式。注意:我不希望jLabel的尺寸发生变化。我只是想改变它的文字。
编辑:
下面是我试过的代码
String text = lnum.getText();
System.out.println(String Text =+ text); // DEBUG
字体originalFont =(Font)lnum.getClientProperty(originalfont); //从客户端属性获取原始字体
if(originalFont == null){//第一次我们调用它:添加
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){//只在需要的时候调整大小
//查看字体宽度可以缩小多少。
double widthRatio =(double)componentWidth /(double)stringWidth;
int newFontSize =(int)Math.floor(originalFont.getSize()* widthRatio); //保持最小大小
//将标签的字体大小设置为新确定的大小。
lnum.setFont(new Font(originalFont.getName(),originalFont.getStyle(),newFontSize));
} else {
lnum.setFont(originalFont); / /调试
}
lnum.setText(text); / /调试
我有一个问题,很多次它不起作用,就像文本是 28885显示-28 ...。
stringWidth = stringWidth + 25; // DEBUG TRY
我不得不添加这段代码,以增加它的长度。这是一个代码,我只是暂时解决这个问题。我想要一个永久的解决方案。 解决方案
改编自您提到的问题的答案:
void setTextFit(JLabel label,String text ){
Font originalFont =(Font)label.getClientProperty(originalfont); //从客户端属性获取原始字体
if(originalFont == null){//第一次我们调用它:添加它
originalFont = label.getFont();
label.putClientProperty(originalfont,originalFont);
}
int stringWidth = label.getFontMetrics(originalFont).stringWidth(text);
int componentWidth = label.getWidth();
if(stringWidth> componentWidth){//只在需要时调整大小
//查看字体宽度可以缩小多少。
double widthRatio =(double)componentWidth /(double)stringWidth;
int newFontSize =(int)Math.floor(originalFont.getSize()* widthRatio); //保持最小大小
//将标签的字体大小设置为新确定的大小。
label.setFont(new Font(originalFont.getName(),originalFont.getStyle(),newFontSize));
} else
label.setFont(originalFont); //文字适合,不要改变字体大小
label.setText(text);
$ b当你显示一个合适的数字时,你应该重置字体(见 else
部分)。
编辑:如果你不行/不行要保留对原始Font的引用,可以将其保存为客户端属性(请参阅第一行)。
There were many posts regarding this problem, but i couldn't understand the answers given by people in there.
Like in this post: "How to change the size of the font of a JLabel to take the maximum size" the answer converts the font size to 14! But that is static and further in other answers; their whole output screen seems to increase.
I display certain numbers in a JLabel named "lnum", it can show numbers upto 3 digits but after that it shows like "4..." I want that if the number is able to fit in the label, it should not change its font size but if like a number is 4 digit, it should decrease the font size in such a way that it fits. NOTE: i do not want that the dimensions of the jLabel change. I just want to change the text in It.
Edit:
Here is what code i tried
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);
I have a problem that many a times it doesn't work, like if the text is "-28885" it shows "-28...".
stringWidth = stringWidth + 25; //DEBUG TRY
I had to add this code so that it increases the length that it gets. It was a code i added to just temporarly fix the problem. I want a permanent solution for this.
解决方案 Adapted from 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);
}
When you'll display a number that would fit, you should reset the Font back to its original (see the else
part).
EDIT: If you can't/don't want to keep a reference to the original Font, you can save it as a "client property" (see the first lines).
这篇关于如果文本不适合JLabel,我想减小字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!