Java实现一种个性化的CheckBox


Java实现一种个性化的CheckBox:

  1. package com.han;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.EventQueue;  
  5. import java.awt.Font;  
  6. import java.awt.FontMetrics;  
  7. import java.awt.Graphics;  
  8. import java.awt.Insets;  
  9. import java.awt.Rectangle;  
  10. import java.awt.event.WindowAdapter;  
  11. import java.awt.event.WindowEvent;  
  12.   
  13. import javax.swing.AbstractButton;  
  14. import javax.swing.ButtonModel;  
  15. import javax.swing.Icon;  
  16. import javax.swing.JCheckBox;  
  17. import javax.swing.JComponent;  
  18. import javax.swing.JDialog;  
  19. import javax.swing.SwingUtilities;  
  20. import javax.swing.plaf.basic.BasicHTML;  
  21. import javax.swing.plaf.metal.MetalCheckBoxUI;  
  22. import javax.swing.plaf.metal.MetalLookAndFeel;  
  23. import javax.swing.text.View;  
  24.   
  25. import sun.swing.SwingUtilities2;  
  26.   
  27. /** 
  28.  * 实现一种个性化的CheckBox 
  29.  * @author HAN 
  30.  * 
  31.  */  
  32. public class CheckBoxCustomized extends JDialog {  
  33.   
  34.     /** 
  35.      *  
  36.      */  
  37.     private static final long serialVersionUID = 5925267340818484608L;  
  38.   
  39.     /** 
  40.      * Launch the application 
  41.      * @param args 
  42.      */  
  43.     public static void main(String args[]) {  
  44.         EventQueue.invokeLater(new Runnable() {  
  45.             public void run() {  
  46.                 try {  
  47.                     CheckBoxCustomized dialog = new CheckBoxCustomized();  
  48.                     dialog.addWindowListener(new WindowAdapter() {  
  49.                         public void windowClosing(WindowEvent e) {  
  50.                             System.exit(0);  
  51.                         }  
  52.                     });  
  53.                     dialog.setVisible(true);  
  54.                 } catch (Exception e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61.     /** 
  62.      * Create the dialog 
  63.      */  
  64.     public CheckBoxCustomized() {  
  65.         super();  
  66.         getContentPane().setLayout(null);  
  67.         setBounds(100100500375);  
  68.   
  69.         final JCheckBox checkBox = new JCheckBox();  
  70.         checkBox.setText("New JCheckBox");  
  71.         checkBox.setBounds(875711826);  
  72.         getContentPane().add(checkBox);  
  73.         //   
  74.         MetalCheckBoxUI ui = new MetalCheckBoxUI(){  
  75.   
  76.             public synchronized void paint(Graphics g, JComponent c) {  
  77.   
  78.                  AbstractButton b = (AbstractButton) c;  
  79.                  ButtonModel model = b.getModel();  
  80.   
  81.                  Dimension size = c.getSize();  
  82.                    
  83.                  Font f = c.getFont();  
  84.                  g.setFont(f);  
  85.                  FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);  
  86.   
  87.                  Rectangle viewRect = new Rectangle(size);  
  88.                  Rectangle iconRect = new Rectangle();  
  89.                  Rectangle textRect = new Rectangle();  
  90.   
  91.                  Insets i = c.getInsets();  
  92.                  viewRect.x += i.left;  
  93.                  viewRect.y += i.top;  
  94.                  viewRect.width -= (i.right + viewRect.x);  
  95.                  viewRect.height -= (i.bottom + viewRect.y);  
  96.   
  97.                  Icon altIcon = b.getIcon();  
  98.   
  99.                  String text = SwingUtilities.layoutCompoundLabel(  
  100.                      c, fm, b.getText(), altIcon != null ? altIcon : getDefaultIcon(),  
  101.                      b.getVerticalAlignment(), b.getHorizontalAlignment(),  
  102.                      b.getVerticalTextPosition(), b.getHorizontalTextPosition(),  
  103.                      viewRect, iconRect, textRect, b.getIconTextGap());  
  104.   
  105.                  // fill background   
  106.                  if(c.isOpaque()) {  
  107.                      g.setColor(b.getBackground());  
  108.                      g.fillRect(0,0, size.width, size.height);  
  109.                  }  
  110.   
  111.   
  112.                  // Paint the radio button   
  113.                  if(altIcon != null) {  
  114.   
  115.                      if(!model.isEnabled()) {  
  116.                          if(model.isSelected()) {  
  117.                             altIcon = b.getDisabledSelectedIcon();  
  118.                          } else {  
  119.                             altIcon = b.getDisabledIcon();  
  120.                          }  
  121.                      } else if(model.isPressed() && model.isArmed()) {  
  122.                          altIcon = b.getPressedIcon();  
  123.                          if(altIcon == null) {  
  124.                              // Use selected icon   
  125.                              altIcon = b.getSelectedIcon();  
  126.                          }  
  127.                      } else if(model.isSelected()) {  
  128.                          if(b.isRolloverEnabled() && model.isRollover()) {  
  129.                                  altIcon = b.getRolloverSelectedIcon();  
  130.                                  if (altIcon == null) {  
  131.                                          altIcon = b.getSelectedIcon();  
  132.                                  }  
  133.                          } else {  
  134.                                  altIcon = b.getSelectedIcon();  
  135.                          }  
  136.                      } else if(b.isRolloverEnabled() && model.isRollover()) {  
  137.                          altIcon = b.getRolloverIcon();  
  138.                      }  
  139.   
  140.                      if(altIcon == null) {  
  141.                          altIcon = b.getIcon();  
  142.                      }  
  143.   
  144.                      altIcon.paintIcon(c, g, iconRect.x, iconRect.y);  
  145.   
  146.                  } else {  
  147.                      System.out.println("here");  
  148.                      // paint the button icon by myself   
  149.                      if (model.isEnabled()) {  
  150.                          if (model.isPressed() && model.isArmed()) {  
  151.                              g.setColor(MetalLookAndFeel.getControlShadow());  
  152.                              g.fillRect(iconRect.x, iconRect.y, 1313);  
  153.                          }  
  154.                          g.setColor(c.getForeground());  
  155.                      } else {  
  156.                          g.setColor(MetalLookAndFeel.getControlShadow());  
  157.                      }  
  158.   
  159.                      if (model.isSelected()) {  
  160.                          int controlSize = 13;  
  161.                          g.fillRect(iconRect.x + 3, iconRect.y + 52,  
  162.                                  controlSize - 8);  
  163.                          g.drawLine(iconRect.x + (controlSize - 4),  
  164.                                  iconRect.y + 3, iconRect.x + 5, iconRect.y  
  165.                                          + (controlSize - 6));  
  166.                          g.drawLine(iconRect.x + (controlSize - 4),  
  167.                                  iconRect.y + 4, iconRect.x + 5, iconRect.y  
  168.                                          + (controlSize - 5));  
  169.                      }  
  170.                  }  
  171.   
  172.   
  173.                  // Draw the Text   
  174.                  if(text != null) {  
  175.                      View v = (View) c.getClientProperty(BasicHTML.propertyKey);  
  176.                      if (v != null) {  
  177.                          v.paint(g, textRect);  
  178.                      } else {  
  179.                         int mnemIndex = b.getDisplayedMnemonicIndex();  
  180.                         if(model.isEnabled()) {  
  181.                             // *** paint the text normally   
  182.                             g.setColor(b.getForeground());  
  183.                         } else {  
  184.                             // *** paint the text disabled   
  185.                             g.setColor(getDisabledTextColor());  
  186.                         }  
  187.                         SwingUtilities2.drawStringUnderlineCharAt(c,g,text,  
  188.                                 mnemIndex, textRect.x, textRect.y + fm.getAscent());  
  189.                     }  
  190.                     if(b.hasFocus() && b.isFocusPainted() &&  
  191.                        textRect.width > 0 && textRect.height > 0 ) {  
  192.                         paintFocus(g,textRect,size);  
  193.                     }  
  194.                  }  
  195.             }  
  196.         };  
  197.           
  198.         checkBox.setUI(ui);  
  199.         System.out.println(checkBox.isFocusPainted());  
  200.         checkBox.setFocusPainted(false); // this is optional   
  201.         System.out.println();  
  202.     }  
  203.   
  204. }  

相关内容