在JLabel的开头放置3个点 [英] Putting 3 dots at the beginning of JLabel
问题描述
当JLabel中的文本太长时,在文本末尾会有可见的3个点.是否可以将它们放在开头?
When the text in JLabel is too long there are visible 3 dots at the end of text. Is it possible to put them at the beginning?
推荐答案
您可以考虑使用FontMetrics
.类,以查看当前字体下的文本长度.
You may consider using FontMetrics
. class to see the length of your text under the current font.
_________________________________
|
| This is some really long text that I want to fit in the small label
|________________________________
^^^ YOUR LABEL ^^^
说您想将较长的文本放入该标签中.
这是您可以做的(这只是一个疯狂的猜测,我正在动态进行中)
Say you want to fit that long text into that label.
Here is what you can do (and this is just a wild guess and I am making this on the fly)
- 从
String
中的三个点...
开始. - 开始向其添加附加字符.
- 获取您的
JLabel
的宽度. - 使用
FontMetrics
来测量文本的长度(以像素为单位),以添加更多字符 - 只要文本的像素长度小于您的
JLabel
的宽度,请继续添加更多字符
- 一旦它变得大于
JLabel
的宽度,就退出循环. - 将此新形成的文本设置为您的
JLabel
的文本
- Start with your three dots
...
in aString
. - Start adding appending characters to it, one by one.
- Get the width of your
JLabel
. - Use
FontMetrics
to measure the length of your text , in pixels, as you append more characters - Keep adding more characters as long as the pixel length of the text is less than the width of your
JLabel
- Once it becomes greater than the width of the
JLabel
, get out of the loop. - Set this newly formed text as the text of your
JLabel
您应该这样结束:
_________________________________
|
| ...This is some really long tex
|________________________________
^^^ YOUR LABEL ^^^
这是开始使用FontMetrics
的简单方法.避免在那里争吵.只需执行接受的答案即可: Java:获取FontMetrics实例的更友好方式
Here is an easy way to get started with FontMetrics
. Avoid the bickering there. Just do what the accepted answer says: Java: Friendlier way to get an instance of FontMetrics
SSCCE 符合OP的实际需求,而不是我的解释
package stack;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class BackwardsDots extends JFrame{
JLabel label = new JLabel(){
@Override
public Dimension getPreferredSize(){
return new Dimension(200,100);
}
};
String text = "This is a design requirement and not my whim";
FontMetrics fm;
Font theFontBeingUsed;
//--------------------------------------------------------------------------------
public BackwardsDots(){
getContentPane().add(label);
pack();
theFontBeingUsed = new Font("Ubuntu",Font.BOLD,14);
fm = Toolkit.getDefaultToolkit().getFontMetrics(theFontBeingUsed);
label.setText(trimmedStringCalculator(text));
label.setToolTipText(text);
label.setBorder(BorderFactory.createDashedBorder(Color.RED));
label.addComponentListener(new ComponentListener(){
@Override
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void componentResized(ComponentEvent arg0) {
label.setText(trimmedStringCalculator(text));
}
@Override
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//--------------------------------------------------------------------------------
private String trimmedStringCalculator(String inputText){
String ellipses = "...";
String textToBeDisplayed = "";
int widthOfJLabel = label.getWidth();
for(int i = text.length()-1; i >= 0; i--){
if(fm.stringWidth(ellipses + textToBeDisplayed) <= widthOfJLabel){
textToBeDisplayed = text.charAt(i) + textToBeDisplayed;
}
}
String finalText;
if(textToBeDisplayed.equals(inputText)){
finalText = inputText;
}else{
finalText = ellipses.concat(textToBeDisplayed);
}
return finalText;
}
//--------------------------------------------------------------------------------
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new BackwardsDots();
}
});
}
}
输出
这篇关于在JLabel的开头放置3个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!