当标签中的文本太长时,JLabel 将其缩写并在末尾添加 ...。如何自定义此文本?
我有缩写为长文本的方法,现在我不知道如何将它传递给 JLabel:
public static String abbreviateMiddle(String str, String middle, int length, int lengthSuffix) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(middle)) {
return str;
}
if (length >= str.length() || length < (middle.length() + 2) || lengthSuffix > length
|| str.length() < lengthSuffix || length - middle.length() < lengthSuffix) {
return str;
}
int targetSting = length - middle.length();
int endOffset = str.length() - lengthSuffix;
int startOffset = targetSting - lengthSuffix;
StrBuilder builder = new StrBuilder(length);
builder.append(str.substring(0, startOffset));
builder.append(middle);
builder.append(str.substring(endOffset));
return builder.toString();
}