这篇文章主要介绍java如何修改JFrame默认字体方式,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
修改默认字体的方法很简单。首先我们随便写一个按钮出来:
import javax.swing.*; public class Test{ static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args){ JFrame jf = new JFrame(); jf.setVisible(true); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel jp = new JPanel(); jf.setcontentPane(jp); JButton jb = new JButton("确定"); jp.add(jb); jf.pack(); } }
接着我们可以加上这么一行:
UIManager.put("Button.font", new java.awt.Font("宋体", 0, 12));
开头的import加上:
import javax.swing.UIManager;
这样就可以改字体了。
字体类型和大小可以随便改,只要电脑里有安装就行了。“0”位置代表的是字体风格,一般不做修改。(如斜体、加粗等,要修改的话请自行查阅API……我也不太记得了。。)
不建议使用网上的个性字体,因为这样做出来的软件程序到别的电脑上用会产生字体方面的问题。而且尽量少用生僻字,以免某些字体库里没有收录对应的字形。
自定义的字体可以进行一下封装:(下面的部分摘自网络)
public class FontClass { public static void loadIndyFont() { UIManager.put("CheckBox.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Tree.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Viewport.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ProgressBar.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("RadioButtonMenuItem.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("FormattedTextField.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ToolBar.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ColorChooser.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ToggleButton.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Panel.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("TextArea.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Menu.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("RadioButtonMenuItem.acceleratorFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("Spinner.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Menu.acceleratorFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("CheckBoxMenuItem.acceleratorFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("TableHeader.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("TextField.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("OptionPane.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("MenuBar.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Button.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Label.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("PasswordField.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("InternalFrame.titleFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("OptionPane.buttonFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("ScrollPane.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("MenuItem.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ToolTip.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("List.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("OptionPane.messageFont", new java.awt.Font("宋体", 0, 12)); UIManager.put("EditorPane.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("Table.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("TabbedPane.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("RadioButton.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("CheckBoxMenuItem.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("TextPane.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("PopupMenu.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("TitledBorder.font", new java.awt.Font("宋体", 0, 12)); UIManager.put("ComboBox.font", new java.awt.Font("宋体", 0, 12)); }
然后我们只需要在我们想要使用的时候,加上一句:
FontClass.loadIndyFont();
就行了~
import java.io.IOException; import java.awt.Font; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.UIManager; import javax.imageio.ImageIO; public class Frame implements WindowListener { public static JFrame frame; public Frame() { initialize(); } private void initialize(){ // 新建窗体 frame = new JFrame(); // 设置窗体自动调节大小 frame.pack(); // 设置窗体位置、大小 frame.setBounds(100, 100, 100, 100); // 设置窗体是否可调节大小 frame.setResizable(false); // 设置窗体布局 frame.getContentPane().setLayout(null); // 设置窗体标题 frame.setTitle("Frame"); // 设置窗体字体 frame.setFont(new Font("黑体", Font.PLAIN, 17)); // 设置窗体在屏幕中央打开 frame.setLocationRelativeTo(null); // 设置窗体默认关闭方式为退出程序 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体图标 frame.setIconImage(ImageIO.read(this.getClass().getResource("/priv/image/image.png"))); // 设置窗体观感(皮肤/主题) UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // 设置窗体是否可见 frame.setVisible(true); // 添加 WindowListener frame.addWindowListener(this); } /** * @description 重写 WindowListener * @param e */ public void windowClosing(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public static void main(String args[]) { new Frame(); } }
以上是“java如何修改JFrame默认字体方式”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。