服务器之家

服务器之家 > 正文

Java Swing组件定制CheckBox示例

时间:2021-03-26 11:22     来源/作者:fearlessMore

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

先来看看运行效果:

Java Swing组件定制CheckBox示例

具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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.jcheckbox;
import javax.swing.jcomponent;
import javax.swing.jframe;
import javax.swing.jpanel;
import javax.swing.painter;
import javax.swing.swingutilities;
import javax.swing.uidefaults;
import javax.swing.uimanager;
import javax.swing.windowconstants;
public class checkboxskindemo {
  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("checkbox")) {
            system.out.println(entry.getkey() + " = "
                + entry.getvalue());
          }
        }
        jframe frame = new jframe("www.zzvips.com - checkbox 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 checkboxdefaults = new uidefaults();
        checkboxdefaults.put("checkbox.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.fillrect(1, 1, w - 4, h - 4);
                g.setcolor(color.light_gray);
                g.drawrect(1, 1, w - 4, h - 4);
              }
            });
        checkboxdefaults.put("checkbox[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.fillrect(1, 1, w - 4, h - 4);
                g.setcolor(color.dark_gray);
                g.drawpolyline(new int[] { 2, w / 3, w - 2 },
                    new int[] { h / 2 - 1, h - 4, 0 }, 3);
                g.setcolor(color.light_gray);
                g.drawrect(1, 1, w - 4, h - 4);
              }
            });
        jcheckbox checkbox = new jcheckbox("mycheckbox");
        panel.add(checkbox);
        checkbox.putclientproperty("nimbus.overrides", checkboxdefaults);
        checkbox.putclientproperty("nimbus.overrides.inheritdefaults",
            false);
        // add a normal themed slider for comparison
        jcheckbox normalcheckbox = new jcheckbox("normalcheckbox");
        panel.add(normalcheckbox);
        frame.getcontentpane().add(panel, borderlayout.center);
        frame.pack();
        frame.setlocationrelativeto(null);
        frame.setvisible(true);
      }
    });
  }
}

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

原文链接:http://blog.csdn.net/gaowen_han/article/details/19754847

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部