需求背景
最近在做的项目显示的字体感觉太丑,于是乎想着DIY改进一下。
查阅资料,总觉得别人写的都不咋地,于是决心写一篇略微完善点的关于项目字体优化方面的文章。
当然,这篇文章不会教你如何使用TrueType去创建属于你自己的字体,而是使用现成的字体库。
读取、使用系统字体回顾
读取系统字体的字体名及字体数量
方法1: 使用GraphicsEnvironment类
- 获取所有字体的family名
package com.forget406.study; import java.awt.GraphicsEnvironment; public class CustomeFontReading { public static void main(String[] args) { String[] fonts = GraphicsEnvironment // GraphicsEnvironment(抽象类) 图形环境类 .getLocalGraphicsEnvironment() // 获取本地图形环境 .getAvailableFontFamilyNames(); // 获取可用字体family名 int fontCount = 0; // 字体数统计 for(String font : fonts) { fontCount ++; System.out.println(font); } System.out.println("系统字体数:" + fontCount); } }
Agency FB Algerian Arial Arial Black Arial Narrow Arial Rounded MT Bold Arial Unicode MS Arvo Baskerville Old Face Bauhaus 93 Bell MT Berlin Sans FB Berlin Sans FB Demi Bernard MT Condensed Blackadder ITC Bodoni MT Bodoni MT Black Bodoni MT Condensed Bodoni MT Poster Compressed Book Antiqua Bookman Old Style Bookshelf Symbol 7 Bradley Hand ITC Britannic Bold Broadway Brush Script MT Calibri Calibri Light Californian FB Calisto MT Cambria Cambria Math Candara Castellar Centaur Century Century Gothic Century Schoolbook Chiller Colonna MT Comic Sans MS Consolas Constantia Cooper Black Copperplate Gothic Bold Copperplate Gothic Light Corbel Courier New Curlz MT Dialog DialogInput Droid Serif Ebrima Edwardian Script ITC Elephant Engravers MT Eras Bold ITC Eras Demi ITC Eras Light ITC Eras Medium ITC Euclid Euclid Extra Euclid Fraktur Euclid Math One Euclid Math Two Euclid Symbol Felix Titling Fences Footlight MT Light Forte Franklin Gothic Book Franklin Gothic Demi Franklin Gothic Demi Cond Franklin Gothic Heavy Franklin Gothic Medium Franklin Gothic Medium Cond Freestyle Script French Script MT Gabriola Gadugi Garamond Georgia Gigi Gill Sans MT Gill Sans MT Condensed Gill Sans MT Ext Condensed Bold Gill Sans Ultra Bold Gill Sans Ultra Bold Condensed Gloucester MT Extra Condensed Goudy Old Style Goudy Stout Haettenschweiler Harlow Solid Italic Harrington High Tower Text Impact Imprint MT Shadow Indie Flower Informal Roman Javanese Text Jokerman Juice ITC Kristen ITC Kunstler Script Leelawadee UI Leelawadee UI Semilight Lobster Lucida Bright Lucida Calligraphy Lucida Console Lucida Fax Lucida Handwriting Lucida Sans Lucida Sans Typewriter Lucida Sans Unicode Magneto Maiandra GD Malgun Gothic Malgun Gothic Semilight Marlett Matura MT Script Capitals Meiryo Meiryo UI Microsoft Himalaya Microsoft JhengHei Microsoft JhengHei Light Microsoft JhengHei UI Microsoft JhengHei UI Light Microsoft MHei Microsoft NeoGothic Microsoft New Tai Lue Microsoft PhagsPa Microsoft Sans Serif Microsoft Tai Le Microsoft YaHei UI Microsoft YaHei UI Light Microsoft Yi Baiti MingLiU-ExtB MingLiU_HKSCS-ExtB Mistral Modern No. 20 Mongolian Baiti Monospaced Monotype Corsiva MS Gothic MS Outlook MS PGothic MS Reference Sans Serif MS Reference Specialty MS UI Gothic MT Extra MT Extra Tiger MV Boli Myanmar Text Niagara Engraved Niagara Solid Nirmala UI Nirmala UI Semilight OCR A Extended Old English Text MT Onyx Open Sans Palace Script MT Palatino Linotype Papyrus Parchment Perpetua Perpetua Titling MT Playbill PMingLiU-ExtB Poiret One Poor Richard Pristina Rage Italic Raleway Ravie Roboto Roboto Condensed Roboto Slab Rockwell Rockwell Condensed Rockwell Extra Bold SansSerif Script MT Bold Segoe MDL2 Assets Segoe Print Segoe Script Segoe UI Segoe UI Black Segoe UI Emoji Segoe UI Historic Segoe UI Light Segoe UI Semibold Segoe UI Semilight Segoe UI Symbol Segoe WP Segoe WP Black Segoe WP Light Segoe WP Semibold Segoe WP SemiLight Serif Showcard Gothic SimSun-ExtB Sitka Banner Sitka Display Sitka Heading Sitka Small Sitka Subheading Sitka Text Snap ITC Stencil Sylfaen Symbol Symbol Tiger Symbol Tiger Expert Tahoma Tempus Sans ITC Tiger Tiger Expert Times New Roman Trebuchet MS Tw Cen MT Tw Cen MT Condensed Tw Cen MT Condensed Extra Bold Verdana Viner Hand ITC Vivaldi Vladimir Script Webdings Wide Latin Wingdings Wingdings 2 Wingdings 3 woziku-wfzt-CN4262 Yu Gothic Yu Gothic Light Yu Gothic Medium Yu Gothic UI Yu Gothic UI Light Yu Gothic UI Semibold Yu Gothic UI Semilight 仿宋 华文中宋 华文仿宋 华文宋体 华文彩云 华文新魏 华文楷体 华文琥珀 华文细黑 华文行楷 华文隶书 宋体 幼圆 微软雅黑 微软雅黑 Light 我字酷无非字体 新宋体 方正兰亭超细黑简体 方正姚体 方正等线 方正舒体 楷体 等线 等线 Light 隶书 黑体 系统字体数:267
- 获取所有字体的名字
package com.forget406.study; import java.awt.Font; import java.awt.GraphicsEnvironment; public class CustomeFontReading4 { public static void main(String[] args) { Font[] fonts = GraphicsEnvironment .getLocalGraphicsEnvironment() .getAllFonts(); for (Font font : fonts) { System.out.println(font.getFontName()); } System.out.println("字体数量:" + fonts.length); } }
Agency FB Agency FB Bold Algerian Arial Arial Black Arial Bold Arial Bold Italic Arial Italic Arial Narrow Arial Narrow Bold Arial Narrow Bold Italic Arial Narrow Italic Arial Rounded MT Bold Arial Unicode MS Arvo Bold Arvo Bold Arvo Bold Italic Arvo-Italic Baskerville Old Face Bauhaus 93 Bell MT Bell MT Bold Bell MT Italic Berlin Sans FB Berlin Sans FB Bold Berlin Sans FB Demi Bold Bernard MT Condensed Blackadder ITC Bodoni MT Bodoni MT Black Bodoni MT Black Italic Bodoni MT Bold Bodoni MT Bold Italic Bodoni MT Condensed Bodoni MT Condensed Bold Bodoni MT Condensed Bold Italic Bodoni MT Condensed Italic Bodoni MT Italic Bodoni MT Poster Compressed Book Antiqua Book Antiqua Bold Book Antiqua Bold Italic Book Antiqua Italic Bookman Old Style Bookman Old Style Bold Bookman Old Style Bold Italic Bookman Old Style Italic Bookshelf Symbol 7 Bradley Hand ITC Britannic Bold Broadway Brush Script MT Italic Calibri Calibri Bold Calibri Bold Italic Calibri Italic Calibri Light Calibri Light Italic Californian FB Californian FB Bold Californian FB Italic Calisto MT Calisto MT Bold Calisto MT Bold Italic Calisto MT Italic Cambria Cambria Bold Cambria Bold Italic Cambria Italic Cambria Math Candara Candara Bold Candara Bold Italic Candara Italic Castellar Centaur Century Century Gothic Century Gothic Bold Century Gothic Bold Italic Century Gothic Italic Century Schoolbook Century Schoolbook Bold Century Schoolbook Bold Italic Century Schoolbook Italic Chiller Colonna MT Comic Sans MS Comic Sans MS Bold Comic Sans MS Bold Italic Comic Sans MS Italic Consolas Consolas Bold Consolas Bold Italic Consolas Italic Constantia Constantia Bold Constantia Bold Italic Constantia Italic Cooper Black Copperplate Gothic Bold Copperplate Gothic Light Corbel Corbel Bold Corbel Bold Italic Corbel Italic Courier New Courier New Bold Courier New Bold Italic Courier New Italic Curlz MT Dialog.bold Dialog.bolditalic Dialog.italic Dialog.plain DialogInput.bold DialogInput.bolditalic DialogInput.italic DialogInput.plain Droid Serif Bold Droid Serif Bold Droid Serif Bold Italic Droid Serif Italic Ebrima Ebrima Bold Edwardian Script ITC Elephant Elephant Italic Engravers MT Eras Bold ITC Eras Demi ITC Eras Light ITC Eras Medium ITC Euclid Euclid Bold Euclid Bold Italic Euclid Extra Euclid Extra Bold Euclid Fraktur Euclid Fraktur Bold Euclid Italic Euclid Math One Euclid Math One Bold Euclid Math Two Euclid Math Two Bold Euclid Symbol Euclid Symbol Bold Euclid Symbol Bold Italic Euclid Symbol Italic Felix Titling Fences Plain Footlight MT Light Forte Franklin Gothic Book Franklin Gothic Book Italic Franklin Gothic Demi Franklin Gothic Demi Cond Franklin Gothic Demi Italic Franklin Gothic Heavy Franklin Gothic Heavy Italic Franklin Gothic Medium Franklin Gothic Medium Cond Franklin Gothic Medium Italic Freestyle Script French Script MT Gabriola Gadugi Gadugi Bold Garamond Garamond Bold Garamond Italic Georgia Georgia Bold Georgia Bold Italic Georgia Italic Gigi Gill Sans MT Gill Sans MT Bold Gill Sans MT Bold Italic Gill Sans MT Condensed Gill Sans MT Ext Condensed Bold Gill Sans MT Italic Gill Sans Ultra Bold Gill Sans Ultra Bold Condensed Gloucester MT Extra Condensed Goudy Old Style Goudy Old Style Bold Goudy Old Style Italic Goudy Stout Haettenschweiler Harlow Solid Italic Harrington High Tower Text High Tower Text Italic Impact Imprint MT Shadow Indie Flower Informal Roman Javanese Text Jokerman Juice ITC Kristen ITC Kunstler Script Leelawadee UI Leelawadee UI Bold Leelawadee UI Semilight Lobster Lucida Bright Regular Lucida Bright Demibold Lucida Bright Demibold Italic Lucida Bright Italic Lucida Bright Regular Lucida Calligraphy Italic Lucida Console Lucida Fax Demibold Lucida Fax Demibold Italic Lucida Fax Italic Lucida Fax Regular Lucida Handwriting Italic Lucida Sans Demibold Lucida Sans Demibold Lucida Sans Demibold Roman Lucida Sans Italic Lucida Sans Regular Lucida Sans Typewriter Bold Lucida Sans Typewriter Bold Lucida Sans Typewriter Oblique Lucida Sans Typewriter Regular Lucida Sans Unicode MS Gothic MS Outlook MS PGothic MS Reference Sans Serif MS Reference Specialty MS UI Gothic MT Extra MT Extra Tiger MV Boli Magneto Bold Maiandra GD Malgun Gothic Malgun Gothic Bold Malgun Gothic Semilight Marlett Matura MT Script Capitals Meiryo Meiryo Bold Meiryo Bold Italic Meiryo Italic Meiryo UI Meiryo UI Bold Meiryo UI Bold Italic Meiryo UI Italic Microsoft Himalaya Microsoft JhengHei Microsoft JhengHei Bold Microsoft JhengHei Light Microsoft JhengHei UI Microsoft JhengHei UI Bold Microsoft JhengHei UI Light Microsoft MHei Microsoft MHei Bold Microsoft NeoGothic Microsoft NeoGothic Bold Microsoft New Tai Lue Microsoft New Tai Lue Bold Microsoft PhagsPa Microsoft PhagsPa Bold Microsoft Sans Serif Microsoft Tai Le Microsoft Tai Le Bold Microsoft YaHei UI Light Microsoft Yahei UI Microsoft Yahei UI Bold Microsoft Yi Baiti MingLiU-ExtB MingLiU_HKSCS-ExtB Mistral Modern No. 20 Mongolian Baiti Monospaced.bold Monospaced.bolditalic Monospaced.italic Monospaced.plain Monotype Corsiva Myanmar Text Myanmar Text Bold 新宋体 Niagara Engraved Niagara Solid Nirmala UI Nirmala UI Bold Nirmala UI Semilight OCR A Extended Old English Text MT Onyx Open Sans Bold Open Sans Bold Open Sans Bold Italic Open Sans Italic PMingLiU-ExtB Palace Script MT Palatino Linotype Palatino Linotype Bold Palatino Linotype Bold Italic Palatino Linotype Italic Papyrus Parchment Perpetua Perpetua Bold Perpetua Bold Italic Perpetua Italic Perpetua Titling MT Bold Perpetua Titling MT Light Playbill Poiret One Poor Richard Pristina Rage Italic Raleway Bold Raleway Bold Ravie Roboto Bold Roboto Bold Roboto Bold Italic Roboto Condensed Bold Roboto Condensed Bold Roboto Condensed Bold Italic Roboto Condensed Italic Roboto Italic Roboto Slab Bold Roboto Slab Regular Rockwell Rockwell Bold Rockwell Bold Italic Rockwell Condensed Rockwell Condensed Bold Rockwell Extra Bold Rockwell Italic SansSerif.bold SansSerif.bolditalic SansSerif.italic SansSerif.plain Script MT Bold Segoe MDL2 Assets Segoe Print Segoe Print Bold Segoe Script Segoe Script Bold Segoe UI Segoe UI Black Segoe UI Black Italic Segoe UI Bold Segoe UI Bold Italic Segoe UI Emoji Segoe UI Historic Segoe UI Italic Segoe UI Light Segoe UI Light Italic Segoe UI Semibold Segoe UI Semibold Italic Segoe UI Semilight Segoe UI Semilight Italic Segoe UI Symbol Segoe WP Segoe WP Black Segoe WP Bold Segoe WP Light Segoe WP SemiLight Segoe WP Semibold Serif.bold Serif.bolditalic Serif.italic Serif.plain Showcard Gothic 宋体 SimSun-ExtB Sitka Banner Sitka Banner Bold Sitka Banner Bold Italic Sitka Banner Italic Sitka Display Sitka Display Bold Sitka Display Bold Italic Sitka Display Italic Sitka Heading Sitka Heading Bold Sitka Heading Bold Italic Sitka Heading Italic Sitka Small Sitka Small Bold Sitka Small Bold Italic Sitka Small Italic Sitka Subheading Sitka Subheading Bold Sitka Subheading Bold Italic Sitka Subheading Italic Sitka Text Sitka Text Bold Sitka Text Bold Italic Sitka Text Italic Snap ITC Stencil Sylfaen Symbol Symbol Tiger Symbol Tiger Expert Tahoma Tahoma Bold Tempus Sans ITC Tiger Tiger Expert Times New Roman Times New Roman Bold Times New Roman Bold Italic Times New Roman Italic Trebuchet MS Trebuchet MS Bold Trebuchet MS Bold Italic Trebuchet MS Italic Tw Cen MT Tw Cen MT Bold Tw Cen MT Bold Italic Tw Cen MT Condensed Tw Cen MT Condensed Bold Tw Cen MT Condensed Extra Bold Tw Cen MT Italic Verdana Verdana Bold Verdana Bold Italic Verdana Italic Viner Hand ITC Vivaldi Italic Vladimir Script Webdings Wide Latin Wingdings Wingdings 2 Wingdings 3 Yu Gothic Bold Yu Gothic Light Yu Gothic Medium Yu Gothic Regular Yu Gothic UI Bold Yu Gothic UI Light Yu Gothic UI Regular Yu Gothic UI Semibold Yu Gothic UI Semilight YuGothic YuGothic Bold 我字酷无非字体 仿宋 华文中宋 华文仿宋 华文宋体 华文彩云 华文新魏 华文楷体 华文琥珀 华文细黑 华文行楷 华文隶书 宋体 幼圆 微软雅黑 微软雅黑 Bold 微软雅黑 Light 我字酷无非字体 新宋体 方正兰亭超细黑简体 方正姚体 方正等线 Regular 方正舒体 楷体 方正等线 Regular 等线 Bold 等线 Light 隶书 黑体 字体数量:479
以上两段代码有细微的区别,根本原因在于:
所见字体文件中包含多个字体子文件 。如下图所示,左侧简体字又分成:常规、粗体、细体,即包含右侧的字体。
方法2: 调用核心类库的文件操作API
- 过滤字体文件
package com.forget406.study; import java.io.File; import java.io.FileFilter; public class CustomeFontReading2 { public static void main(String[] args) { // windows10 系统字体库目录 File file = new File("C:" + File.separator+"Windows"+ File.separator+"Fonts" ); // 文件信息判断 if (file.exists()) { if (file.isDirectory()) { System.out.println("该路径为文件夹"); // 获取目录下所有以".ttf"(True Type Font)结尾的文件 File[] fontFile = file.listFiles(new FileFilter() { // 过滤出名字符合要求的文件 @Override public boolean accept(File file) { return file.getName().endsWith("."); } }); // 输出所有字体文件名(截去后缀) for(File font : fontFile) { String name = font.getName(); int suffixIndex = name.lastIndexOf(".ttf"); System.out.println(name.substring(0, suffixIndex)); } System.out.println("字体数量:" + fontFile.length); } else if (file.isFile()){ System.out.println("该路径为文件"); } else { System.out.println("貌似没有别的可能了"); } System.out.println(file.getName()); } else { System.out.println("文件路径不存在"); } } }
该路径为文件夹 arial arialbd arialbi ariali ariblk Arvo-Bold Arvo-BoldItalic Arvo-Italic Arvo-Regular calibri calibrib calibrii calibril calibrili calibriz cambriab cambriai cambriaz Candara Candarab Candarai Candaraz comic comicbd comici comicz consola consolab consolai consolaz constan constanb constani constanz corbel corbelb corbeli corbelz cour courbd courbi couri Deng Dengb Dengl DengXian DroidSerif-Bold DroidSerif-BoldItalic DroidSerif-Italic DroidSerif ebrima ebrimabd euclid euclidb euclidbi euclidi eucsym eucsymb eucsymbi eucsymi euextra euextrab eufrak eufrakb eumat1 eumat1b eumat2 eumat2b fences framd framdit Gabriola gadugi gadugib georgia georgiab georgiai georgiaz himalaya impact IndieFlower javatext LeelaUIb LeelawUI LeelUIsl Lobster-Regular lucon l_10646 malgun malgunbd malgunsl marlett micross mmrtext mmrtextb monbaiti MSMHei-Bold MSMHei MSNeoGothic-Bold MSNeoGothic msyi Mt Extra Tiger mtextra mvboli Nirmala NirmalaB NirmalaS ntailu ntailub OpenSans-Bold OpenSans-BoldItalic OpenSans-Italic OpenSans-Regular pala palab palabi palai phagspa phagspab PoiretOne-Regular Raleway-Bold Raleway-Regular Roboto-Bold Roboto-BoldItalic Roboto-Italic Roboto-Regular RobotoCondensed-Bold RobotoCondensed-BoldItalic RobotoCondensed-Italic RobotoCondensed-Regular RobotoSlab-Bold RobotoSlab-Regular segmdl2 segoepr segoeprb segoesc segoescb segoeui segoeuib segoeuii segoeuil segoeuisl segoeuiz SegoeWP-Black SegoeWP-Bold SegoeWP-Light SegoeWP-Semibold SegoeWP-Semilight SegoeWP seguibl seguibli seguiemj seguihis seguili seguisb seguisbi seguisli seguisym simfang simhei simkai simsunb sylfaen Symbol Tiger Expert Symbol Tiger symbol tahoma tahomabd taile taileb Tiger Expert Tiger times timesbd timesbi timesi trebuc trebucbd trebucbi trebucit verdana verdanab verdanai verdanaz webdings wingding YuGothic-Bold YuGothic 我字酷无非字体 我字酷无非字体_0 我字酷无非字体_1 字体数量:191
注意:windows 10系统中字体文件后缀(suffix)含有三种:ttf、.TTF、.ttc、fon。
- 不过滤字体文件
由于c:\Windows\Font文件夹下除字体文件外不含其它形式文件,因此可以不对文件结果进行过滤。
package com.forget406.study; import java.io.File; import java.io.FileFilter; public class CustomeFontReading3 { public static void main(String[] args) { // windows10 系统字体库目录 File file = new File("C:" + File.separator+"Windows"+ File.separator+"Fonts" ); // 文件信息判断 if (file.exists()) { if (file.isDirectory()) { File[] fontFile = file.listFiles(); for(File font : fontFile) { String name = font.getName(); int suffixIndex = name.lastIndexOf("."); System.out.println(name.substring(0, suffixIndex)); } System.out.println("字体数量:" + fontFile.length); } else if (file.isFile()){ System.out.println("该路径为文件"); } else { System.out.println("貌似没有别的可能了"); } } else { System.out.println("文件路径不存在"); } } }
该路径为文件夹 8514fix 8514fixe 8514fixg 8514fixr 8514fixt 8514oem 8514oeme 8514oemg 8514oemr 8514oemt 8514sys 8514syse 8514sysg 8514sysr 8514syst 85775 85855 85f1255 85f1256 85f1257 85f874 85s1255 85s1256 85s1257 85s874 AGENCYB AGENCYR ALGER ANTQUAB ANTQUABI ANTQUAI app775 app850 app852 app855 app857 app866 app932 app936 app949 app950 arial arialbd arialbi ariali ARIALN ARIALNB ARIALNBI ARIALNI ARIALUNI ariblk ARLRDBD Arvo-Bold Arvo-BoldItalic Arvo-Italic Arvo-Regular BASKVILL BAUHS93 BELL BELLB BELLI BERNHC BKANT BOD_B BOD_BI BOD_BLAI BOD_BLAR BOD_CB BOD_CBI BOD_CI BOD_CR BOD_I BOD_PSTC BOD_R BOOKOS BOOKOSB BOOKOSBI BOOKOSI BRADHITC BRITANIC BRLNSB BRLNSDB BRLNSR BROADW BRUSHSCI BSSYM7 c8514fix c8514oem c8514sys calibri calibrib calibrii calibril calibrili calibriz CALIFB CALIFI CALIFR CALIST CALISTB CALISTBI CALISTI cambria cambriab cambriai cambriaz Candara Candarab Candarai Candaraz CASTELAR CENSCBK CENTAUR CENTURY cga40737 cga40850 cga40852 cga40857 cga40866 cga40869 cga40woa cga80737 cga80850 cga80852 cga80857 cga80866 cga80869 cga80woa CHILLER COLONNA comic comicbd comici comicz consola consolab consolai consolaz constan constanb constani constanz COOPBL COPRGTB COPRGTL corbel corbelb corbeli corbelz coue1255 coue1256 coue1257 couf1255 couf1256 couf1257 cour courbd courbi coure couree coureg courer couret courf courfe courfg courfr courft couri CURLZ___ cvgafix cvgasys Deng Dengb Dengl DengXian desktop dos737 dos869 dosapp DroidSerif-Bold DroidSerif-BoldItalic DroidSerif-Italic DroidSerif ebrima ebrimabd ega40737 ega40850 ega40852 ega40857 ega40866 ega40869 ega40woa ega80737 ega80850 ega80852 ega80857 ega80866 ega80869 ega80woa ELEPHNT ELEPHNTI ENGR ERASBD ERASDEMI ERASLGHT ERASMD euclid euclidb euclidbi euclidi eucsym eucsymb eucsymbi eucsymi euextra euextrab eufrak eufrakb eumat1 eumat1b eumat2 eumat2b FELIXTI fences fms_metadata FORTE FRABK FRABKIT FRADM FRADMCN FRADMIT FRAHV FRAHVIT framd FRAMDCN framdit FREESCPT FRSCRIPT FTLTLT FZLTCXHJW FZSTK FZYTK Gabriola gadugi gadugib GARA GARABD GARAIT georgia georgiab georgiai georgiaz GIGI GILBI___ GILB____ GILC____ GILI____ GILLUBCD GILSANUB GIL_____ GLECB GlobalMonospace GlobalSansSerif GlobalSerif GlobalUserInterface GLSNECB GOTHIC GOTHICB GOTHICBI GOTHICI GOUDOS GOUDOSB GOUDOSI GOUDYSTO h8514fix h8514oem h8514sys HARLOWSI HARNGTON HATTEN himalaya HTOWERT HTOWERTI hvgafix hvgasys impact IMPRISHA IndieFlower INFROMAN ITCBLKAD ITCEDSCR ITCKRIST j8514fix j8514oem j8514sys javatext JOKERMAN jsmalle jsmallf JUICE___ jvgafix jvgasys KUNSTLER LATINWD LBRITE LBRITED LBRITEDI LBRITEI LCALLIG LeelaUIb LeelawUI LeelUIsl LFAX LFAXD LFAXDI LFAXI LHANDW Lobster-Regular LSANS LSANSD LSANSDI LSANSI LTYPE LTYPEB LTYPEBO LTYPEO lucon l_10646 MAGNETOB MAIAN malgun malgunbd malgunsl marlett MATURASC MEIRYO MEIRYOB micross mingliub MISTRAL mmrtext mmrtextb MOD20 modern monbaiti msgothic msjh msjhbd msjhl MSMHei-Bold MSMHei MSNeoGothic-Bold MSNeoGothic msyh msyhbd msyhl msyi Mt Extra Tiger MTCORSVA mtextra mvboli NIAGENG NIAGSOL Nirmala NirmalaB NirmalaS ntailu ntailub OCRAEXT OLDENGL ONYX OpenSans-Bold OpenSans-BoldItalic OpenSans-Italic OpenSans-Regular OUTLOOK pala palab palabi palai PALSCRI PAPYRUS PARCHM PERBI___ PERB____ PERI____ PERTIBD PERTILI PER_____ phagspa phagspab PLAYBILL PoiretOne-Regular POORICH PRISTINA RAGE Raleway-Bold Raleway-Regular RAVIE REFSAN REFSPCL Roboto-Bold Roboto-BoldItalic Roboto-Italic Roboto-Regular RobotoCondensed-Bold RobotoCondensed-BoldItalic RobotoCondensed-Italic RobotoCondensed-Regular RobotoSlab-Bold RobotoSlab-Regular ROCCB___ ROCC____ ROCK ROCKB ROCKBI ROCKEB ROCKI roman s8514fix s8514oem s8514sys SCHLBKB SCHLBKBI SCHLBKI script SCRIPTBL segmdl2 segoepr segoeprb segoesc segoescb segoeui segoeuib segoeuii segoeuil segoeuisl segoeuiz SegoeWP-Black SegoeWP-Bold SegoeWP-Light SegoeWP-Semibold SegoeWP-Semilight SegoeWP seguibl seguibli seguiemj seguihis seguili seguisb seguisbi seguisli seguisym sere1255 sere1256 sere1257 serf1255 serf1256 serf1257 serife serifee serifeg serifer serifet seriff seriffe seriffg seriffr serifft SHOWG simfang simhei simkai SIMLI simsun simsunb SIMYOU Sitka SitkaB SitkaI SitkaZ smae1255 smae1256 smae1257 smaf1255 smaf1256 smaf1257 smalle smallee smalleg smaller smallet smallf smallfe smallfg smallfr smallft SNAP____ ssee1255 ssee1256 ssee1257 ssee874 ssef1255 ssef1256 ssef1257 ssef874 sserife sserifee sserifeg sserifer sserifet sseriff sseriffe sseriffg sseriffr sserifft StaticCache STCAIYUN STENCIL STFANGSO STHUPO STKAITI STLITI STSONG STXIHEI STXINGKA STXINWEI STZHONGS svgafix svgasys sylfaen Symbol Tiger Expert Symbol Tiger symbol tahoma tahomabd taile taileb TCBI____ TCB_____ TCCB____ TCCEB TCCM____ TCMI____ TCM_____ TEMPSITC Tiger Expert Tiger times timesbd timesbi timesi trebuc trebucbd trebucbi trebucit verdana verdanab verdanai verdanaz vga737 vga775 vga850 vga852 vga855 vga857 vga860 vga861 vga863 vga865 vga866 vga869 vga932 vga936 vga949 vga950 vgaf1255 vgaf1256 vgaf1257 vgaf874 vgafix vgafixe vgafixg vgafixr vgafixt vgaoem vgas1255 vgas1256 vgas1257 vgas874 vgasys vgasyse vgasysg vgasysr vgasyst VINERITC VIVALDII VLADIMIR webdings wingding WINGDNG2 WINGDNG3 YuGothB YuGothic-Bold YuGothic YuGothL YuGothM YuGothR 我字酷无非字体 我字酷无非字体_0 我字酷无非字体_1 字体数量:612
系统字体名字都用英文字母表示(不含中文)。自定义字体安装到操作系统中根据实际取名情况而定。
比较两种方式的区别
GraphicsEnvironment类 | 实现对字体(常规、粗细、斜体等)封装,即粗略分类 | 读取系统字体Java推荐 |
核心类库文件操作API | 读取所有字体文件,即详细分类(包含.fon字体文件) | 读取系统字体时不推荐 |
使用系统字体在屏幕显示
package com.forget406.study; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; public class CustomeFontReading extends JFrame { public CustomeFontReading() { setTitle("自定义字体使用"); setSize(300, 400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); MyPanel panel = new MyPanel(); add(panel); } public static void main(String[] args) { new CustomeFontReading(); } } class MyPanel extends JPanel { @Override public void paint(Graphics g) { g.setColor(Color.BLUE); g.setFont(new Font("楷体", Font.BOLD, 50)); g.drawString("和谐社会", 30, 200); } }
程序运行结果
将自定义字体导入系统字体库,也能够通过这种方法使用自定义字体。
引入外部自定义字体
字体下载网站传送门
【分享】全球最大的字体下载网站:www.fontke.com(字客网)
创建、注册外部字体
以自己项目中使用过的字体文件Deeko Comic Regular.ttf为例,加以说明。以下是此字体对应的参数说明。
在使用外部字体时,必须先创建字体,然后在操作系统中注册外部字体;否则无法使用外部字体。
package com.forget406.study; import java.awt.Color; import java.awt.Font; import java.awt.FontFormatException; import java.awt.Graphics; import java.awt.GraphicsEnvironment; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; public class CustomeFontReading extends JFrame { private static final long serialVersionUID = 270583473364792225L; public CustomeFontReading() { setTitle("自定义字体使用"); setSize(300, 400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); MyPanel panel = new MyPanel(); add(panel); } public static void main(String[] args) { new CustomeFontReading(); } } class MyPanel extends JPanel { private static final long serialVersionUID = 4897720234933257563L; @Override public void paint(Graphics g) { g.setColor(Color.BLUE); registerCustomeFont("Deeko Comic Regular.ttf"); // 画笔使用自定义字体(字体风格、尺寸可与创建时参数不同) g.setFont(new Font("Deeko Comic Regular", Font.BOLD, 30)); g.drawString("HARMONY", 30, 200); } private void registerCustomeFont(String filename) { File fontFile = new File( "font"+File.separator+filename); System.out.println(fontFile.exists()); Font font = null; try { // 创建自定义字体 font = Font.createFont( Font.TRUETYPE_FONT, fontFile); // 字体.ttf格式 // 设置字体风格、尺寸 font.deriveFont(Font.BOLD,30F); GraphicsEnvironment ge = // 获得本地图形环境对象 GraphicsEnvironment .getLocalGraphicsEnvironment(); // 在本地图形环境中注册当前字体 boolean register = ge.registerFont(font); if(register) { System.out.println("注册字体成功"); } else { System.out.println("注册字体失败"); } System.out.println("外部字体创建成功"); } catch (FontFormatException e) { e.printStackTrace(); System.out.println("字体格式化异常"); } catch (IOException e) { e.printStackTrace(); System.out.println("读写字体文件异常"); } } }
程序测试结果
除了使用File,还能够使用InputStream。但就性能而言,推荐使用File。
来源:https://www.cnblogs.com/forget406/p/5830294.html