温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java Swing组件定制CheckBox示例

发布时间:2020-10-08 16:33:39 来源:脚本之家 阅读:187 作者:fearlessMore 栏目:编程语言

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

先来看看运行效果:

Java Swing组件定制CheckBox示例

具体代码如下:

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.jb51.net - 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算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI