Need Swing applicaiton program set unification Font? Answer is Yes, if you think that default Font which set by System is not good enough or you want to really control the size of application. If your application runs on several language operation system, you may find that the font are not same between them, and sometimes you can see "... " on UI instead of normal charactors as the special size is too small to show all of them, though they are shown normally on another machine with different language.
So here I give a simple solution to set Java Application font:
package com.sss.util;
import java.awt.Font;
import javax.swing.UIManager;
/**
* @create Aug 10, 2009 4:26:41 PM
*/
public class FontManager {
private Font appFont = null;
public FontManager(Font font){
appFont = font;
setAppFont();
}
/**
* Set Application font with that specified in construction parameter.
* If there is not specified font, use default font
*/
public void setAppFont(){
if(appFont != null){
setAppFont(appFont);
}
}
/**
* Set Application Font
* @param font
*/
public void setAppFont(Font font){
// Font font = new Font("",Font.PLAIN,14);
UIManager.put("Button.font",font);
UIManager.put("ToggleButton.font",font);
UIManager.put("RadioButton.font",font);
UIManager.put("CheckBox.font",font);
UIManager.put("ColorChooser.font",font);
UIManager.put("ToggleButton.font",font);
UIManager.put("ComboBox.font",font);
UIManager.put("ComboBoxItem.font",font);
UIManager.put("InternalFrame.titleFont",font);
UIManager.put("Label.font",font);
UIManager.put("List.font",font);
UIManager.put("MenuBar.font",font);
UIManager.put("Menu.font",font);
UIManager.put("MenuItem.font",font);
UIManager.put("RadioButtonMenuItem.font",font);
UIManager.put("CheckBoxMenuItem.font",font);
UIManager.put("PopupMenu.font",font);
UIManager.put("OptionPane.font",font);
UIManager.put("OptionPane.messageFont",font);
UIManager.put("OptionPane.buttonFont",font);
UIManager.put("Panel.font",font);
UIManager.put("ProgressBar.font",font);
UIManager.put("ScrollPane.font",font);
UIManager.put("Viewport",font);
UIManager.put("TabbedPane.font",font);
UIManager.put("TableHeader.font",font);
UIManager.put("TextField.font",font);
UIManager.put("PasswordFiled.font",font);
UIManager.put("TextArea.font",font);
UIManager.put("TextPane.font",font);
UIManager.put("EditorPane.font",font);
UIManager.put("TitledBorder.font",font);
UIManager.put("ToolBar.font",font);
UIManager.put("ToolTip.font",font);
UIManager.put("Tree.font",font);
UIManager.put("Table.font",font);
}
}
You can use it with below code:
Font font = new Font("Tomaho", Font.PLAIN, 10);
FontManager fontManager = new FontManager(font);
fontManager. setAppFont();
本文介绍了一种在Java Swing应用程序中统一设置字体的方法,通过创建FontManager类来解决跨语言操作系统下字体显示不一致的问题。
2425

被折叠的 条评论
为什么被折叠?



