当前位置: 首页>>代码示例>>Java>>正文


Java JComponent.getFontMetrics方法代码示例

本文整理汇总了Java中javax.swing.JComponent.getFontMetrics方法的典型用法代码示例。如果您正苦于以下问题:Java JComponent.getFontMetrics方法的具体用法?Java JComponent.getFontMetrics怎么用?Java JComponent.getFontMetrics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JComponent的用法示例。


在下文中一共展示了JComponent.getFontMetrics方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testStringWidth

import javax.swing.JComponent; //导入方法依赖的package包/类
private static void testStringWidth() {

        String str = "12345678910\u036F";
        JComponent comp = createComponent(str);
        Font font = comp.getFont();
        FontMetrics fontMetrics = comp.getFontMetrics(font);
        float stringWidth = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        if (stringWidth == fontMetrics.stringWidth(str)) {
            throw new RuntimeException("Numeric shaper is not used!");
        }

        if (stringWidth != getLayoutWidth(str, font, NUMERIC_SHAPER)) {
            throw new RuntimeException("Wrong text width!");
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:bug8132119.java

示例2: testStringClip

import javax.swing.JComponent; //导入方法依赖的package包/类
private static void testStringClip() {

        String str = "1234567890";
        JComponent comp = createComponent(str);
        FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

        int width = (int) BasicGraphicsUtils.getStringWidth(comp, fontMetrics, str);

        String clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, width + 1);
        checkClippedString(str, clip, str);

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, -1);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics, str, 0);
        checkClippedString(str, clip, "...");

        clip = BasicGraphicsUtils.getClippedString(comp, fontMetrics,
                str, width - width / str.length());
        int endIndex = str.length() - 3;
        checkClippedString(str, clip, str.substring(0, endIndex) + "...");
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:bug8132119.java

示例3: checkNullArgumentsGetClippedString

import javax.swing.JComponent; //导入方法依赖的package包/类
private static void checkNullArgumentsGetClippedString(
        JComponent comp, String text) {

    FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());

    BasicGraphicsUtils.getClippedString(null, fontMetrics, text, 1);
    String result = BasicGraphicsUtils.getClippedString(comp, fontMetrics, null, 1);
    if (!"".equals(result)) {
        throw new RuntimeException("Empty string is not returned!");
    }

    try {
        BasicGraphicsUtils.getClippedString(comp, null, text, 1);
    } catch (NullPointerException e) {
        return;
    }

    throw new RuntimeException("NPE is not thrown");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:bug8132119.java

示例4: checkNullArgumentsGetStringWidth

import javax.swing.JComponent; //导入方法依赖的package包/类
private static void checkNullArgumentsGetStringWidth(JComponent comp,
        String text) {

    FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
    BasicGraphicsUtils.getStringWidth(null, fontMetrics, text);
    float result = BasicGraphicsUtils.getStringWidth(comp, fontMetrics, null);

    if (result != 0) {
        throw new RuntimeException("The string length is not 0");
    }

    try {
        BasicGraphicsUtils.getStringWidth(comp, null, text);
    } catch (NullPointerException e) {
        return;
    }

    throw new RuntimeException("NPE is not thrown");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:bug8132119.java

示例5: getTxtFontMetrics

import javax.swing.JComponent; //导入方法依赖的package包/类
protected final FontMetrics getTxtFontMetrics() {
    if (fm == null) {
        JComponent control = getDisplayer();
        fm = control.getFontMetrics(getTxtFont());
    }
    return fm;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:AbstractViewTabDisplayerUI.java

示例6: computeWidth

import javax.swing.JComponent; //导入方法依赖的package包/类
/** Computes width of string up to maxCharCount, with font of given JComponent
 * and with maximum percentage of owning Window that can be taken */
private static int computeWidth (JComponent comp, int maxCharCount, int percent) {
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    int charW = fm.charWidth('X');
    int result = charW * maxCharCount;
    // limit width to 50% of containing window
    Window w = SwingUtilities.windowForComponent(comp);
    if (w != null) {
        result = Math.min(result, w.getWidth() * percent / 100);
    }
    return result;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:QuickSearchPopup.java

示例7: computeFitText

import javax.swing.JComponent; //导入方法依赖的package包/类
public static String computeFitText(JComponent component, int maxWidth, String text, boolean bold) {
    if (text == null) {
        text = ""; // NOI18N
    }
    if (text.length() <= VISIBLE_START_CHARS + 3) {
        return text;
    }
    FontMetrics fm;
    if (bold) {
        fm = component.getFontMetrics(component.getFont().deriveFont(Font.BOLD));
    } else {
        fm = component.getFontMetrics(component.getFont());
    }
    int width = maxWidth;

    String sufix = "..."; // NOI18N
    int sufixLength = fm.stringWidth(sufix + " "); //NOI18N
    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) {
            if (fm.stringWidth(text.substring(i + 1)) <= fm.stringWidth(sufix)) {
                return text;
            }
            return prefix.length() > 0 ? prefix + sufix : text;
        }
    }
    return text;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:35,代码来源:DashboardUtils.java

示例8: testDrawString

import javax.swing.JComponent; //导入方法依赖的package包/类
private static void testDrawString(boolean underlined) {
    String str = "AOB";
    JComponent comp = createComponent(str);

    BufferedImage buffImage = createBufferedImage(WIDTH, HEIGHT);
    Graphics2D g2 = buffImage.createGraphics();

    g2.setColor(DRAW_COLOR);
    g2.setFont(comp.getFont());

    FontMetrics fontMetrices = comp.getFontMetrics(comp.getFont());
    float width = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, str);
    float x = (WIDTH - width) / 2;
    int y = 3 * HEIGHT / 4;

    if (underlined) {
        BasicGraphicsUtils.drawStringUnderlineCharAt(comp, g2, str, 1, x, y);
    } else {
        BasicGraphicsUtils.drawString(comp, g2, str, x, y);
    }
    g2.dispose();

    float xx = BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "A") +
            BasicGraphicsUtils.getStringWidth(comp, fontMetrices, "O")/2;

    checkImageContainsSymbol(buffImage, (int) xx, underlined ? 3 : 2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:bug8132119.java

示例9: getColumnWidthInPixels

import javax.swing.JComponent; //导入方法依赖的package包/类
public static int getColumnWidthInPixels(String str, JComponent comp) {
    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    return fm.stringWidth(str);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:5,代码来源:UIUtils.java


注:本文中的javax.swing.JComponent.getFontMetrics方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。