In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces java swing how to load custom fonts, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Java swing loads custom fonts
In actual development, we need to make an one-to-one mapping between the font name and the font, and then we need to load the custom font in a configurable way. So there is this need, let's realize it.
First, we define a utility class that customizes the loading subclass.
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; / * font utility class, get the required font * / public class FontUtil {/ * all font configurations * / private static Map fontNameMap = new HashMap () / * * default font size * / private static final float defaultFontSize = 20f; static {/ / load configuration file Properties properties = new Properties (); / / load input stream using properties object, encoding using GBK try {properties.load (new InputStreamReader (FontUtil.class.getClassLoader (). GetResourceAsStream ("font.properties"), "GBK")) } catch (IOException e) {System.err.println ("font.properties profile does not exist");} / / get the value corresponding to key for (Map.Entry entry: properties.entrySet ()) {Object key = entry.getKey (); Object value = entry.getValue () If (key! = null & & value! = null) {fontNameMap.put (String.valueOf (key), String.valueOf (value)) } / * get the name of the defined font * * @ param key font * @ return * / public static Font getConfigFont (String key) {return getConfigFont (key, defaultFontSize) } / * get the custom font * * @ param key font name * @ param fontSize font size * @ return * / public static Font getConfigFont (String key, float fontSize) {String fontUrl = fontNameMap.get (key) If (fontUrl = = null) {throw new RuntimeException (the font configuration of "name is" + key + "does not exist");} / / defaults to see if it is the system font Font font = new Font (fontUrl, Font.PLAIN, (int) fontSize) / / determine whether if ("Dialog.plain" .equals (font.getFontName () {try (InputStream is = new FileInputStream (new File (fontUrl));) {Font definedFont = Font.createFont (Font.TRUETYPE_FONT, is) exists in the current font. / / set font size, float definedFont = definedFont.deriveFont (fontSize); return definedFont;} catch (Exception e) {throw new RuntimeException ("font whose name is" + key + "does not exist");}} return font;}}
The second part is to write test code:
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");}}
Run, the fourth font does not exist, throw an exception, everything else is handled normally, An and B all load their own configured fonts.
Environment configuration, create a new font configuration file in resources: font.properties content is as follows:
Configuration file for # font, with the font name before the equal sign and the path to the font after the equal sign A=D:/logs/ Apple boldface-quasi-.ttf B=D:/logs/ Apple boldface-medium bold-simplified .ttf C = Arial D = Arial 22222
Originally is to help others write the code, finally do not want, directly open source out.
Java swing changes global fonts
This code is called before the jframe is displayed, such as when the main method starts:
Public static void setUIFont () {Font f = new Font ("Song style", 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)}} Thank you for reading this article carefully. I hope the article "how to load custom fonts in java swing" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.