java swing 實現(xiàn)加載自定義的字體
java swing 加載自定義的字體
在實際開發(fā)中, 我們需要把字體的名字和字體做一一對應(yīng)的映射關(guān)系, 然后需要通過可配置的方式加載自定義的字體. 所以就有了這個需求, 我們來實現(xiàn)。
首先我們定義一個自定義加載子類的工具類
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 字體工具類, 獲取需要的字體
*/
public class FontUtil {
/**
* 所有字體配置
*/
private static Map<String, String> fontNameMap = new HashMap<String, String>();
/**
* 默認(rèn)字體的大小
*/
private static final float defaultFontSize = 20f;
static {
//加載配置文件
Properties properties = new Properties();
// 使用properties對象加載輸入流, 編碼使用GBK
try {
properties.load(new InputStreamReader(FontUtil.class.getClassLoader().getResourceAsStream("font.properties"), "GBK"));
} catch (IOException e) {
System.err.println("font.properties 配置文件不存在");
}
//獲取key對應(yīng)的value值
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null && value != null) {
fontNameMap.put(String.valueOf(key), String.valueOf(value));
}
}
}
/**
* 獲取定義的字體
*
* @param key 字體的名字
* @return
*/
public static Font getConfigFont(String key) {
return getConfigFont(key, defaultFontSize);
}
/**
* 獲取自定義的字體
*
* @param key 字體的名字
* @param fontSize 字體的大小
* @return
*/
public static Font getConfigFont(String key, float fontSize) {
String fontUrl = fontNameMap.get(key);
if (fontUrl == null) {
throw new RuntimeException("名字是:" + key + "的字體配置不存在");
}
//默認(rèn)先看是不是系統(tǒng)字體
Font font = new Font(fontUrl, Font.PLAIN, (int) fontSize);
//判斷當(dāng)前字體存不存在
if ("Dialog.plain".equals(font.getFontName())) {
try (
InputStream is = new FileInputStream(new File(fontUrl));
) {
Font definedFont = Font.createFont(Font.TRUETYPE_FONT, is);
//設(shè)置字體大小,float型
definedFont = definedFont.deriveFont(fontSize);
return definedFont;
} catch (Exception e) {
throw new RuntimeException("名字是:" + key + "的字體不存在");
}
}
return font;
}
}
第二部再就是寫測試代碼:
import java.awt.*;
public class Demo {
public static void main(String[] args) throws Exception {
Font a = FontUtil.getConfigFont("A");
System.out.println(a.getName() + "~" + a.getSize());
Font b = FontUtil.getConfigFont("B", 100);
System.out.println(b.getName() + "~" + b.getSize());
Font c = FontUtil.getConfigFont("C");
System.out.println(c.getFontName());
Font d = FontUtil.getConfigFont("D");
}
}
運行, 第四個字體不存在, 拋出異常 , 其他的都正常處理了, A, B都加載了自己配置的字體.
環(huán)境配置, 在resources里面新建一個字體配置文件: font.properties 內(nèi)容如下:
#字體的配置文件,等號前是字體名字,等號后是字體的路徑 A=D:/logs/蘋方黑體-準(zhǔn)-簡.ttf B=D:/logs/蘋方黑體-中粗-簡.ttf C=宋體 D=宋體22222
本來是幫別人寫的代碼, 最后不要了, 就直接開源出來了.
Java swing更改全局字體
這段代碼在jframe顯示前調(diào)用,比如main方法開始就調(diào)用:
public static void setUIFont()
{
Font f = new Font("宋體",Font.PLAIN,18);
String names[]={ "Label", "CheckBox", "PopupMenu","MenuItem", "CheckBoxMenuItem",
"JRadioButtonMenuItem","ComboBox", "Button", "Tree", "ScrollPane",
"TabbedPane", "EditorPane", "TitledBorder", "Menu", "TextArea",
"OptionPane", "MenuBar", "ToolBar", "ToggleButton", "ToolTip",
"ProgressBar", "TableHeader", "Panel", "List", "ColorChooser",
"PasswordField","TextField", "Table", "Label", "Viewport",
"RadioButtonMenuItem","RadioButton", "DesktopPane", "InternalFrame"
};
for (String item : names) {
UIManager.put(item+ ".font",f);
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Swing圖形界面實現(xiàn)可動態(tài)刷新的驗證碼
這篇文章主要為大家詳細介紹了Swing圖形界面實現(xiàn)可動態(tài)刷新的驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
java實現(xiàn)Xml與json之間的相互轉(zhuǎn)換操作示例
這篇文章主要介紹了java實現(xiàn)Xml與json之間的相互轉(zhuǎn)換操作,結(jié)合實例形式分析了Java xml與json相互轉(zhuǎn)換工具類的定義與使用相關(guān)操作技巧,需要的朋友可以參考下2019-06-06

