成都创新互联网站制作重庆分公司

JavaSwing组件定制RadioButton示例-创新互联

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

创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为鹤庆企业提供专业的网站制作、网站设计鹤庆网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

先来看看效果图:

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 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() {
              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() {
              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);
      }
    });
  }
}


分享题目:JavaSwing组件定制RadioButton示例-创新互联
网页地址:http://cxhlcq.com/article/dcoeid.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部