java修改JFrame默認字體方式
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();
就行瞭~
JFrame基本參數設置
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(); } }
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。