Swing控件(JLabel,JButton....)设置字体和大小

来源:互联网 发布:淘宝求好评短信模板 编辑:程序博客网 时间:2024/06/05 21:17
有很多方法可以给Swing的控件设置字体和样式,我这里介绍一种直观的。 
首先我们要定义一种字体例如: 
Font f = new Font("隶书",Font.PLAIN,15); 

然后我们利用UIManager全局的给控件设置样式,例如: 
UIManager.put("Label.font",font); 
这样我们就给所有的JLabel设置了字体的样式即15号隶书。 

这样的好处就是我们可以利用一个全局的方法,精确控制各种控件的字体和大小。 

当然关于JLabel,您还可以用更灵活的方法,JLabel里的内容是支持html标签的。例如: 
Java代码  收藏代码
  1. JLabel demoLabel = new JLabel();  
  2. //demoLabel.setText("<b>这是一号标题</b>");  
  3. //demoLabel.setText("<li>这是一号标题<li>");  
  4. demoLabel.setText("<h1>这是一号标题</h1>");  


下面是UIManager的详细参数 
Java代码  收藏代码
  1. Font f = new Font("隶书",Font.PLAIN,15);  
  2. UIManager.put("Button.font",font);  
  3. UIManager.put("ToggleButton.font",font);  
  4. UIManager.put("RadioButton.font",font);  
  5. UIManager.put("CheckBox.font",font);  
  6. UIManager.put("ColorChooser.font",font);  
  7. UIManager.put("ToggleButton.font",font);  
  8. UIManager.put("ComboBox.font",font);  
  9. UIManager.put("ComboBoxItem.font",font);  
  10. UIManager.put("InternalFrame.titleFont",font);  
  11. UIManager.put("Label.font",font);  
  12. UIManager.put("List.font",font);  
  13. UIManager.put("MenuBar.font",font);  
  14. UIManager.put("Menu.font",font);  
  15. UIManager.put("MenuItem.font",font);  
  16. UIManager.put("RadioButtonMenuItem.font",font);  
  17. UIManager.put("CheckBoxMenuItem.font",font);  
  18. UIManager.put("PopupMenu.font",font);  
  19. UIManager.put("OptionPane.font",font);  
  20. UIManager.put("Panel.font",font);  
  21. UIManager.put("ProgressBar.font",font);  
  22. UIManager.put("ScrollPane.font",font);  
  23. UIManager.put("Viewport",font);  
  24. UIManager.put("TabbedPane.font",font);  
  25. UIManager.put("TableHeader.font",font);  
  26. UIManager.put("TextField.font",font);  
  27. UIManager.put("PasswordFiled.font",font);  
  28. UIManager.put("TextArea.font",font);  
  29. UIManager.put("TextPane.font",font);  
  30. UIManager.put("EditorPane.font",font);  
  31. UIManager.put("TitledBorder.font",font);  
  32. UIManager.put("ToolBar.font",font);  
  33. UIManager.put("ToolTip.font",font);  
  34. UIManager.put("Tree.font",font);  


文章地址:http://javapub.iteye.com/blog/753739
原创粉丝点击