- 论坛徽章:
- 0
|
如何更改Java程序的默认字体?
可以试一下这个程序:
- import java.awt.Font;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.UIManager;
- import javax.swing.plaf.FontUIResource;
- public class DefaultFontTest {
- public static void setUIFont(javax.swing.plaf.FontUIResource f) {
- //
- // sets the default font for all Swing components.
- // ex.
- // setUIFont (new
- // javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
- //
- java.util.Enumeration keys = UIManager.getDefaults().keys();
- while (keys.hasMoreElements()) {
- Object key = keys.nextElement();
- Object value = UIManager.get(key);
- if (value instanceof javax.swing.plaf.FontUIResource)
- UIManager.put(key, f);
- }
- }
- public static void main(String[] args) {
- setUIFont(new FontUIResource("Comic Sans MS", Font.BOLD, 20));
- JFrame jf = new JFrame("DefaultFontTest");
- JLabel jl = new JLabel("The font of this line should have been changed");
- jf.getContentPane().add(jl);
- jf.setSize(500, 100);
- jf.show();
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
复制代码 |
|