Java Swing查看字体和设置全局字体
查看支持的字体
以下代码用于运行时在控制台打印支持的字体
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
final String AvailableFontFamilyNames[] = gEnv.getAvailableFontFamilyNames();
Stream.of(AvailableFontFamilyNames).forEach(System.out::println);
设置全局字体
private void initGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}