下面列出了javax.swing.JLabel#getFontMetrics ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
void setLabelText(JLabel label, String text) {
StringBuilder builder = new StringBuilder("<html>");
char[] chars = text.toCharArray();
FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
int start = 0;
int len = 0;
while (start + len < text.length()) {
while (true) {
len++;
if (start + len > text.length())
break;
if (fontMetrics.charsWidth(chars, start, len) > label.getWidth()) {
break;
}
}
builder.append(chars, start, len - 1).append("<br/>");
start = start + len - 1;
len = 0;
}
builder.append(chars, start, text.length() - start);
builder.append("</html>");
label.setText(builder.toString());
}
private static String computeFitText(JLabel label) {
String text = label.getText();
if(text == null) text = "";
if (text.length() <= VISIBLE_START_CHARS + 3) return text;
Icon icon = label.getIcon();
int iconWidth = icon != null ? icon.getIconWidth() : 0;
FontMetrics fm = label.getFontMetrics(label.getFont());
int width = label.getSize().width - iconWidth;
String sufix = "..."; // NOI18N
int sufixLength = fm.stringWidth(sufix);
int desired = width - sufixLength;
if (desired <= 0) return text;
for (int i = 0; i <= text.length() - 1; i++) {
String prefix = text.substring(0, i);
int swidth = fm.stringWidth(prefix);
if (swidth >= desired) {
return prefix.length() > 0 ? prefix + sufix: text;
}
}
return text;
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
protected void calculateGeometry(JLabel label) {
FontMetrics fm = label.getFontMetrics(label.getFont());
String text = label.getText();
Icon icon = label.getIcon();
int verticalAlignment = label.getVerticalAlignment();
int horizontalAlignment = label.getHorizontalAlignment();
int verticalTextPosition = label.getVerticalTextPosition();
int horizontalTextPosition = label.getHorizontalTextPosition();
int textIconGap = label.getIconTextGap();
viewR.setFrame(0, 0, getViewWidth(label), label.getHeight());
SwingUtilities.layoutCompoundLabel(fm, text, icon, verticalAlignment,
horizontalAlignment, verticalTextPosition,
horizontalTextPosition, viewR, iconRect, textRect, textIconGap);
textRect.x = label.getWidth() / 2 - textRect.width / 2;
iconRect.x = label.getWidth() / 2 - iconRect.width / 2;
iconRect.y = 0;
textRect.y = iconRect.height + label.getIconTextGap();
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private static void leak() {
Map<TextAttribute, Object> textAttributes = new HashMap<>();
textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
textAttributes.put(TextAttribute.SIZE, 12);
textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
Font font = Font.getFont(textAttributes);
JLabel label = new JLabel();
int dummy = 0;
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
for (int j = 0; j <1000; j++) {
FontMetrics fm = label.getFontMetrics(font);
dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
}
}
System.out.println("done " + dummy);
}
private String processTextToFit (JLabel lbl, String text)
{
if (text == null)
return "" ;
FontMetrics fm = lbl.getFontMetrics(lbl.getFont());
if (fm == null)
return text ;
int textWidth = fm.stringWidth(text);
int lblWidth = lbl.getWidth() ;
if (lblWidth >= textWidth)
return text ;
// we assume that the set of all characters have a mean length equal or smaller than 'Aa' / 2
String dots = " ... " ;
int charWidth = fm.stringWidth("Aa") / 2;
int dotsWidth = fm.stringWidth(dots);
int maxNumChar = (lblWidth - dotsWidth) / charWidth ;
int blockWidth = maxNumChar / 2 ;
if (blockWidth <= 0)
return text ;
StringBuilder sb = new StringBuilder () ;
sb.append(text.substring(0, blockWidth)) ;
sb.append(dots) ;
sb.append(text.substring(text.length() - blockWidth)) ;
return sb.toString() ;
}
/**
* Adjusts the column width of the row headers table containg the number
* column. The font metrics are extracted from the label of the row at the
* bottom of the viewport and used to determing the appropriate width.
*
* The reason why this method is important, is that when the row number increases by an extra digit
* the column needs to get wider. It also needs to shrink when scrolling to smaller digit numbers.
*
* @param rowHeadersTable - single column table in the row header
* @param label - label used to get font metrics
* @param scrollBarValue - int value for determing point of lowest row
*/
private static void adjustColumnWidth(final JTable rowHeadersTable, final JLabel label, int scrollBarValue) {
label.setFont(rowHeadersTable.getFont());
label.setOpaque(true);
label.setHorizontalAlignment(JLabel.CENTER);
int v = rowHeadersTable.getVisibleRect().height;
int row = rowHeadersTable.rowAtPoint(new Point(0, v + scrollBarValue));
Integer modelValue = null;
if (row != -1) {
modelValue = (Integer) rowHeadersTable.getModel().getValueAt(row, 0);
} else {
RowHeadersTableModel tm = (RowHeadersTableModel) rowHeadersTable.getModel();
modelValue = new Integer(tm.getMaxIntValue());
}
label.setText("" + modelValue);
FontMetrics fontMetrics = label.getFontMetrics(label.getFont());
int widthFactor = 0;
if (fontMetrics != null && label.getText() != null) {
widthFactor = fontMetrics.stringWidth(label.getText());
rowHeadersTable.setPreferredScrollableViewportSize(new Dimension(widthFactor + 8, 100)); // height
// is
// ignored
rowHeadersTable.repaint();
}
}
public Component getTableCellRendererComponent(JTable table,
Object obj,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JLabel label = (JLabel)super.getTableCellRendererComponent(table,
obj,
isSelected,
hasFocus,
row, column);
String s = obj != null ? obj.toString() : "";
int w = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
FontMetrics fm = label.getFontMetrics(label.getFont());
int fmW = fm.stringWidth(s);
// s = w + "/" + fmW + ":" + s;
int nChars = 3;
int m = s.length() / 2;
String s2 = s;
while((fmW+10) > w && s2.length() > nChars && s2.length() > 5) {
int n1 = nChars / 2;
int n2 = nChars - n1;
String left = s.substring(0, m - n1);
String right = s.substring(m + n2);
s2 = left + "..." + right;
fmW = fm.stringWidth(s2);
nChars++;
}
label.setText(s2);
return label;
}
/**
* 根据文本的式样,以及文本内容,获取文本在屏幕上展示的像素宽
*
* @param font
* 字体式样
* @param text
* 文本
* @return 文本宽度
*/
public final static int getTextWidth(Font font, String text) {
JLabel label = new JLabel(text);
label.setFont(font);
FontMetrics metrics = label.getFontMetrics(label.getFont());
return metrics.stringWidth(label.getText());
}