当标签中的文字太长时,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();
}