本文整理匯總了Java中javax.swing.plaf.basic.BasicGraphicsUtils.getStringWidth方法的典型用法代碼示例。如果您正苦於以下問題:Java BasicGraphicsUtils.getStringWidth方法的具體用法?Java BasicGraphicsUtils.getStringWidth怎麽用?Java BasicGraphicsUtils.getStringWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.plaf.basic.BasicGraphicsUtils
的用法示例。
在下文中一共展示了BasicGraphicsUtils.getStringWidth方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testStringWidth
import javax.swing.plaf.basic.BasicGraphicsUtils; //導入方法依賴的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!");
}
}
示例2: testStringClip
import javax.swing.plaf.basic.BasicGraphicsUtils; //導入方法依賴的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) + "...");
}
示例3: checkNullArgumentsGetStringWidth
import javax.swing.plaf.basic.BasicGraphicsUtils; //導入方法依賴的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");
}
示例4: testDrawString
import javax.swing.plaf.basic.BasicGraphicsUtils; //導入方法依賴的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);
}