javax.swing.JLabel#getFontMetrics ( )源码实例Demo

下面列出了javax.swing.JLabel#getFontMetrics ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dragonwell8_jdk   文件: KerningLeak.java
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);
}
 
源代码2 项目: TencentKona-8   文件: KerningLeak.java
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);
}
 
源代码3 项目: jdk8u60   文件: KerningLeak.java
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);
}
 
源代码4 项目: openjdk-jdk8u   文件: KerningLeak.java
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);
}
 
源代码5 项目: SmartIM   文件: PopupPanel.java
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());
}
 
源代码6 项目: netbeans   文件: QueryTableCellRenderer.java
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;
}
 
源代码7 项目: openjdk-jdk8u-backup   文件: KerningLeak.java
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);
}
 
源代码8 项目: openjdk-jdk9   文件: KerningLeak.java
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);
}
 
源代码9 项目: jdk8u-jdk   文件: KerningLeak.java
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);
}
 
源代码10 项目: hottub   文件: KerningLeak.java
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);
}
 
源代码11 项目: openjdk-8-source   文件: KerningLeak.java
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);
}
 
源代码12 项目: openjdk-8   文件: KerningLeak.java
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);
}
 
源代码13 项目: pumpernickel   文件: ThumbnailLabelUI.java
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();
}
 
源代码14 项目: jdk8u_jdk   文件: KerningLeak.java
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);
}
 
源代码15 项目: jdk8u-jdk   文件: KerningLeak.java
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);
}
 
源代码16 项目: jdk8u-dev-jdk   文件: KerningLeak.java
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);
}
 
源代码17 项目: swift-explorer   文件: ProgressPanel.java
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() ;
}
 
源代码18 项目: swing_library   文件: TableRowUtilities.java
/**
 * 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();
	}
}
 
源代码19 项目: knopflerfish.org   文件: StringCellRenderer.java
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;
}
 
源代码20 项目: FoxBPM   文件: SVGUtils.java
/**
 * 根据文本的式样,以及文本内容,获取文本在屏幕上展示的像素宽
 * 
 * @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());
}