查看: 1243|回复: 0

[Java学习] Java Swing组件定制RadioButton示例

发表于 2018-3-3 12:06:29

本文实例讲述了Java Swing组件定制RadioButton。分享给大家供大家参考,具体如下:

先来看看效果图:

具体代码如下:

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

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。



回复

使用道具 举报