欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java Swing组件定制RadioButton示例

时间:2020-11-23 12:24:48|栏目:JAVA代码|点击:

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

先来看看效果图:

具体代码如下:

package themedemo;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.Painter;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class RadioButtonSkinDemo {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        for (UIManager.LookAndFeelInfo laf : UIManager
            .getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            try {
              UIManager.setLookAndFeel(laf.getClassName());
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
        for (Map.Entry<Object, Object> entry : UIManager
            .getLookAndFeelDefaults().entrySet()) {
          if ((entry.getKey().toString()).startsWith("RadioButton")) {
            System.out.println(entry.getKey() + " = "
                + entry.getValue());
          }
        }
        JFrame frame = new JFrame(
            "www.jb51.net - RadioButton Skining Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        JPanel panel = new JPanel(new GridLayout(0, 1, 20, 20));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        panel.setBackground(Color.darkGray);
        UIDefaults radioButtonDefaults = new UIDefaults();
        radioButtonDefaults.put("RadioButton.iconPainter",
            new Painter<JComponent>() {
              public void paint(Graphics2D g, JComponent c,
                  int w, int h) {
                g.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                g.setStroke(new BasicStroke(2f));
                g.setColor(Color.WHITE);
                g.fillOval(1, 1, w - 4, h - 4);
                g.setColor(Color.LIGHT_GRAY);
                g.drawOval(1, 1, w - 4, h - 4);
              }
            });
        radioButtonDefaults.put("RadioButton[Selected].iconPainter",
            new Painter<JComponent>() {
              public void paint(Graphics2D g, JComponent c,
                  int w, int h) {
                g.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                g.setStroke(new BasicStroke(2f));
                g.setColor(Color.WHITE);
                g.fillOval(1, 1, w - 4, h - 4);
                g.setColor(Color.DARK_GRAY);
                g.fillOval(5, 5, w - 11, h - 11);
                g.setColor(Color.LIGHT_GRAY);
                g.drawOval(1, 1, w - 4, h - 4);
              }
            });
        JRadioButton radioButton = new JRadioButton("myRadioButton");
        panel.add(radioButton);
        radioButton.putClientProperty("Nimbus.Overrides",
            radioButtonDefaults);
        radioButton.putClientProperty(
            "Nimbus.Overrides.InheritDefaults", false);
        // Add a normal themed slider for comparison
        JRadioButton normalRadionButton = new JRadioButton(
            "normalRadionButton");
        panel.add(normalRadionButton);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }
}

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

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

上一篇:java中封装的实现方法详解

栏    目:JAVA代码

下一篇:Spring多对象引入方法

本文标题:Java Swing组件定制RadioButton示例

本文地址:http://www.codeinn.net/misctech/25480.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有