java – 更改默认的JLabel字体

前端之家收集整理的这篇文章主要介绍了java – 更改默认的JLabel字体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我该如何设置所有JLabel实例的默认字体.而不是单独为每个JLabel设置字体.

解决方法

使用 UIManager定义JLabel的默认字体:
  1. import java.awt.FlowLayout;
  2. import java.awt.Font;
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.UIManager;
  6.  
  7. public class LabelFont {
  8.  
  9. public static void main(String[] args) {
  10. Font oldLabelFont = UIManager.getFont("Label.font");
  11. UIManager.put("Label.font",oldLabelFont.deriveFont(Font.PLAIN));
  12.  
  13. JFrame f = new JFrame("LabelFont Test");
  14. f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  15. f.getContentPane().setLayout(new FlowLayout());
  16.  
  17. JLabel df = new JLabel("Default JLabel font");
  18. f.getContentPane().add(df);
  19.  
  20. JLabel ef = new JLabel("Font explicitly set");
  21. ef.setFont(oldLabelFont);
  22. f.getContentPane().add(ef);
  23.  
  24. f.pack();
  25. f.setVisible(true);
  26. }
  27. }

途经:http://coding.derkeiler.com/Archive/Java/comp.lang.java.help/2005-04/msg00395.html

猜你在找的Java相关文章