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.dhdzp.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);
}
});
}
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
詳解如何在Java中重寫equals()和hashCode()方法
在 Java 中,equals() 和 hashCode() 方法是 Object 類中定義的重要方法,它們用于比較對象的相等性以及計算對象的哈希值,本文將詳細介紹如何在 Java 中重寫 equals() 和 hashCode() 方法,并討論其最佳實踐,需要的朋友可以參考下2024-08-08
使用java8 API遍歷過濾文件目錄及子目錄和隱藏文件示例詳解
這篇文章主要介紹了使用java8API遍歷過濾文件目錄及子目錄及隱藏文件示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Mybatis使用update更新值為null時不生效問題解決
這篇文章主要介紹了Mybatis使用update更新值為null時不生效問題解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
SpringBoot實現(xiàn)HTTP調(diào)用的7 種方式
本文主要介紹了SpringBoot實現(xiàn)HTTP調(diào)用的7 種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-04-04
ByteArrayOutputStream簡介和使用_動力節(jié)點Java學院整理
ByteArrayOutputStream 是字節(jié)數(shù)組輸出流。它繼承于OutputStream。這篇文章主要介紹了ByteArrayOutputStream簡介和使用,需要的朋友可以參考下2017-05-05
SpringCloud Alibaba Seata (收藏版)
Seata是一款開源的分布式事務解決方案,致力于在微服務架構(gòu)在提供高性能和簡單一樣的分布式事務服務。這篇文章主要介紹了SpringCloud Alibaba Seata 的相關(guān)知識,需要的朋友可以參考下2020-10-10
SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法
這篇文章主要介紹了SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
java代碼塊之簡易qq登錄界面及按鈕顏色設(shè)置代碼
這篇文章主要介紹了java代碼塊之簡易qq登錄界面及按鈕顏色設(shè)置代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11

