歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java實現一種個性化的CheckBox

Java實現一種個性化的CheckBox

日期:2017/3/1 10:16:07   编辑:Linux編程

Java實現一種個性化的CheckBox:

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