java Modify the default font mode of JFrame
- 2021-12-11 17:56:59
- OfStack
java Modify JFrame Default Font
The way to modify the default font is simple. First of all, let's write a button casually:
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(" Determine ");
jp.add(jb);
jf.pack();
}
}
Then we can add this line:
UIManager.put("Button.font", new java.awt.Font(" Song Style ", 0, 12));
Add import at the beginning:
import javax.swing.UIManager;
So you can change the font.
The font type and size can be changed at will, as long as it is installed in the computer. The position of "0" represents the font style, and 1 is generally not modified. (For example, italics, bold, etc., please refer to API for modification... I don't quite remember...)
It is not recommended to use personalized fonts on the Internet, because the software program made in this way will cause font problems when used on other computers. And try to use rare words as little as possible, so as not to include corresponding glyphs in some font libraries.
Custom fonts can be encapsulated in 1: (The following part is taken from the network)
public class FontClass {
public static void loadIndyFont() {
UIManager.put("CheckBox.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Tree.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Viewport.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ProgressBar.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("RadioButtonMenuItem.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("FormattedTextField.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ToolBar.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ColorChooser.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ToggleButton.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Panel.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TextArea.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Menu.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("RadioButtonMenuItem.acceleratorFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Spinner.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Menu.acceleratorFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("CheckBoxMenuItem.acceleratorFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TableHeader.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TextField.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("OptionPane.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("MenuBar.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Button.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Label.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("PasswordField.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("InternalFrame.titleFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("OptionPane.buttonFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ScrollPane.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("MenuItem.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ToolTip.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("List.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("OptionPane.messageFont", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("EditorPane.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("Table.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TabbedPane.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("RadioButton.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("CheckBoxMenuItem.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TextPane.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("PopupMenu.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("TitledBorder.font", new java.awt.Font(" Song Style ", 0, 12));
UIManager.put("ComboBox.font", new java.awt.Font(" Song Style ", 0, 12));
}
Then we just need to add one sentence when we want to use it:
FontClass.loadIndyFont();
Just do it ~
JFrame Basic Parameter Settings
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(){
// New Form
frame = new JFrame();
// Set the form to automatically resize
frame.pack();
// Setting Form Position, Size
frame.setBounds(100, 100, 100, 100);
// Set whether the form is resizable
frame.setResizable(false);
// Setting Form Layout
frame.getContentPane().setLayout(null);
// Set the form title
frame.setTitle("Frame");
// Set the form font
frame.setFont(new Font(" Blackbody ", Font.PLAIN, 17));
// Set the form to open in the center of the screen
frame.setLocationRelativeTo(null);
// Set the default closing mode of the form to exit the program
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Setting Form Icons
frame.setIconImage(ImageIO.read(this.getClass().getResource("/priv/image/image.png")));
// Set the form look and feel (skin / Subject)
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// Set whether the form is visible
frame.setVisible(true);
// Add WindowListener
frame.addWindowListener(this);
}
/**
* @description Rewrite 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();
}
}