I want to set different font weights for components on my JFrame dialog. How do I do this?
In the below Java statement
setFont(new Font("Dialog", Font.BOLD, 12));
when I use Font.BOLD it is too bold and when I use Font.Plain it is too plain. I want something in-between.
welle is partially correct. You can use TextAttributes to obtain a font:
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, Font.DIALOG);
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, 12);
label.setFont(Font.getFont(attributes));
A better approach is to derive your font from the font installed on the Swing component by the look-and-feel:
Font font = label.getFont();
font = font.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD));
label.setFont(font);
That will preserve the font's family and size, which users may have set in their desktop preferences for readability reasons.
maybe i wrong but i think class Font has only Bold ,plain but you can change after that in number
setFont(new Font("Dialog", Font.BOLD, 12));
setFont(new Font("Dialog", Font.plain, 27));
but in class java.awt.font.TextAttribute
you have WEIGHT_BOLD and WEIGHT_SEMIBOLD ...
解决方案是按名称加载字体的变体,例如:
Font font = new Font("Segoe UI Semibold", Font.PLAIN, 12);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.