欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Swing组件定制RadioButton示例

程序员文章站 2023-11-29 08:55:22
本文实例讲述了java swing组件定制radiobutton。分享给大家供大家参考,具体如下: 先来看看效果图: 具体代码如下: package th...

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

先来看看效果图:

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程序设计有所帮助。