Swing定制CheckBox(转存)

[java]  view plain  copy
  1. package themedemo;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Graphics2D;  
  7. import java.awt.GridLayout;  
  8. import java.awt.RenderingHints;  
  9. import java.util.Map;  
  10.   
  11. import javax.swing.BorderFactory;  
  12. import javax.swing.JCheckBox;  
  13. import javax.swing.JComponent;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JPanel;  
  16. import javax.swing.Painter;  
  17. import javax.swing.SwingUtilities;  
  18. import javax.swing.UIDefaults;  
  19. import javax.swing.UIManager;  
  20. import javax.swing.WindowConstants;  
  21.   
  22. public class CheckBoxSkinDemo {  
  23.     public static void main(String[] args) {  
  24.         SwingUtilities.invokeLater(new Runnable() {  
  25.             public void run() {  
  26.                 for (UIManager.LookAndFeelInfo laf : UIManager  
  27.                         .getInstalledLookAndFeels()) {  
  28.                     if ("Nimbus".equals(laf.getName())) {  
  29.                         try {  
  30.                             UIManager.setLookAndFeel(laf.getClassName());  
  31.                         } catch (Exception e) {  
  32.                             e.printStackTrace();  
  33.                         }  
  34.                     }  
  35.                 }  
  36.   
  37.                 for (Map.Entry<Object, Object> entry : UIManager  
  38.                         .getLookAndFeelDefaults().entrySet()) {  
  39.                     if ((entry.getKey().toString()).startsWith("CheckBox")) {  
  40.                         System.out.println(entry.getKey() + " = "  
  41.                                 + entry.getValue());  
  42.                     }  
  43.                 }  
  44.   
  45.                 JFrame frame = new JFrame("CheckBox Skining Demo");  
  46.                 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  47.                 frame.getContentPane().setLayout(new BorderLayout());  
  48.                 JPanel panel = new JPanel(new GridLayout(012020));  
  49.                 panel.setBorder(BorderFactory.createEmptyBorder(20202020));  
  50.                 panel.setBackground(Color.darkGray);  
  51.   
  52.                 UIDefaults checkBoxDefaults = new UIDefaults();  
  53.                 checkBoxDefaults.put("CheckBox.iconPainter",  
  54.                         new Painter<JComponent>() {  
  55.                             public void paint(Graphics2D g, JComponent c,  
  56.                                     int w, int h) {  
  57.                                 g.setRenderingHint(  
  58.                                         RenderingHints.KEY_ANTIALIASING,  
  59.                                         RenderingHints.VALUE_ANTIALIAS_ON);  
  60.                                 g.setStroke(new BasicStroke(2f));  
  61.                                 g.setColor(Color.WHITE);  
  62.                                 g.fillRect(11, w - 4, h - 4);  
  63.                                 g.setColor(Color.LIGHT_GRAY);  
  64.                                 g.drawRect(11, w - 4, h - 4);  
  65.                             }  
  66.                         });  
  67.                 checkBoxDefaults.put("CheckBox[Selected].iconPainter",  
  68.                         new Painter<JComponent>() {  
  69.                             public void paint(Graphics2D g, JComponent c,  
  70.                                     int w, int h) {  
  71.                                 g.setRenderingHint(  
  72.                                         RenderingHints.KEY_ANTIALIASING,  
  73.                                         RenderingHints.VALUE_ANTIALIAS_ON);  
  74.                                 g.setStroke(new BasicStroke(2f));  
  75.                                 g.setColor(Color.WHITE);  
  76.                                 g.fillRect(11, w - 4, h - 4);  
  77.                                 g.setColor(Color.DARK_GRAY);  
  78.                                 g.drawPolyline(new int[] { 2, w / 3, w - 2 },  
  79.                                         new int[] { h / 2 - 1, h - 40 }, 3);  
  80.                                 g.setColor(Color.LIGHT_GRAY);  
  81.                                 g.drawRect(11, w - 4, h - 4);  
  82.                             }  
  83.                         });  
  84.   
  85.                 JCheckBox checkBox = new JCheckBox("myCheckBox");  
  86.                 panel.add(checkBox);  
  87.                 checkBox.putClientProperty("Nimbus.Overrides", checkBoxDefaults);  
  88.                 checkBox.putClientProperty("Nimbus.Overrides.InheritDefaults",  
  89.                         false);  
  90.   
  91.                 // Add a normal themed slider for comparison  
  92.                 JCheckBox normalCheckBox = new JCheckBox("normalCheckBox");  
  93.                 panel.add(normalCheckBox);  
  94.   
  95.                 frame.getContentPane().add(panel, BorderLayout.CENTER);  
  96.                 frame.pack();  
  97.                 frame.setLocationRelativeTo(null);  
  98.                 frame.setVisible(true);  
  99.             }  
  100.         });  
  101.     }  
  102. }  
[java]  view plain  copy
  1. package themedemo;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.Color;  
  6. import java.awt.Graphics2D;  
  7. import java.awt.GridLayout;  
  8. import java.awt.RenderingHints;  
  9. import java.util.Map;  
  10.   
  11. import javax.swing.BorderFactory;  
  12. import javax.swing.JCheckBox;  
  13. import javax.swing.JComponent;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JPanel;  
  16. import javax.swing.Painter;  
  17. import javax.swing.SwingUtilities;  
  18. import javax.swing.UIDefaults;  
  19. import javax.swing.UIManager;  
  20. import javax.swing.WindowConstants;  
  21.   
  22. public class CheckBoxSkinDemo {  
  23.     public static void main(String[] args) {  
  24.         SwingUtilities.invokeLater(new Runnable() {  
  25.             public void run() {  
  26.                 for (UIManager.LookAndFeelInfo laf : UIManager  
  27.                         .getInstalledLookAndFeels()) {  
  28.                     if ("Nimbus".equals(laf.getName())) {  
  29.                         try {  
  30.                             UIManager.setLookAndFeel(laf.getClassName());  
  31.                         } catch (Exception e) {  
  32.                             e.printStackTrace();  
  33.                         }  
  34.                     }  
  35.                 }  
  36.   
  37.                 for (Map.Entry<Object, Object> entry : UIManager  
  38.                         .getLookAndFeelDefaults().entrySet()) {  
  39.                     if ((entry.getKey().toString()).startsWith("CheckBox")) {  
  40.                         System.out.println(entry.getKey() + " = "  
  41.                                 + entry.getValue());  
  42.                     }  
  43.                 }  
  44.   
  45.                 JFrame frame = new JFrame("CheckBox Skining Demo");  
  46.                 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  47.                 frame.getContentPane().setLayout(new BorderLayout());  
  48.                 JPanel panel = new JPanel(new GridLayout(012020));  
  49.                 panel.setBorder(BorderFactory.createEmptyBorder(20202020));  
  50.                 panel.setBackground(Color.darkGray);  
  51.   
  52.                 UIDefaults checkBoxDefaults = new UIDefaults();  
  53.                 checkBoxDefaults.put("CheckBox.iconPainter",  
  54.                         new Painter<JComponent>() {  
  55.                             public void paint(Graphics2D g, JComponent c,  
  56.                                     int w, int h) {  
  57.                                 g.setRenderingHint(  
  58.                                         RenderingHints.KEY_ANTIALIASING,  
  59.                                         RenderingHints.VALUE_ANTIALIAS_ON);  
  60.                                 g.setStroke(new BasicStroke(2f));  
  61.                                 g.setColor(Color.WHITE);  
  62.                                 g.fillRect(11, w - 4, h - 4);  
  63.                                 g.setColor(Color.LIGHT_GRAY);  
  64.                                 g.drawRect(11, w - 4, h - 4);  
  65.                             }  
  66.                         });  
  67.                 checkBoxDefaults.put("CheckBox[Selected].iconPainter",  
  68.                         new Painter<JComponent>() {  
  69.                             public void paint(Graphics2D g, JComponent c,  
  70.                                     int w, int h) {  
  71.                                 g.setRenderingHint(  
  72.                                         RenderingHints.KEY_ANTIALIASING,  
  73.                                         RenderingHints.VALUE_ANTIALIAS_ON);  
  74.                                 g.setStroke(new BasicStroke(2f));  
  75.                                 g.setColor(Color.WHITE);  
  76.                                 g.fillRect(11, w - 4, h - 4);  
  77.                                 g.setColor(Color.DARK_GRAY);  
  78.                                 g.drawPolyline(new int[] { 2, w / 3, w - 2 },  
  79.                                         new int[] { h / 2 - 1, h - 40 }, 3);  
  80.                                 g.setColor(Color.LIGHT_GRAY);  
  81.                                 g.drawRect(11, w - 4, h - 4);  
  82.                             }  
  83.                         });  
  84.   
  85.                 JCheckBox checkBox = new JCheckBox("myCheckBox");  
  86.                 panel.add(checkBox);  
  87.                 checkBox.putClientProperty("Nimbus.Overrides", checkBoxDefaults);  
  88.                 checkBox.putClientProperty("Nimbus.Overrides.InheritDefaults",  
  89.                         false);  
  90.   
  91.                 // Add a normal themed slider for comparison  
  92.                 JCheckBox normalCheckBox = new JCheckBox("normalCheckBox");  
  93.                 panel.add(normalCheckBox);  
  94.   
  95.                 frame.getContentPane().add(panel, BorderLayout.CENTER);  
  96.                 frame.pack();  
  97.                 frame.setLocationRelativeTo(null);  
  98.                 frame.setVisible(true);  
  99.             }  
  100.         });  
  101.     }  
  102. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值