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.dbjr.com.cn - 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); } }); } }
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
IDEA連接MySQL提示serverTimezone的問題及解決方法
很多朋友私聊小編,使用IDEA軟件連接MySQL數(shù)據(jù)庫時總是提示Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.的錯誤,小編就不一一回復(fù)大家了,下面小編把我的解決方法分享到腳本之家平臺,需要的朋友參考下吧2021-05-05解決@NonNull @org.jetbrains.annotations.NotNull報紅的問題
這篇文章主要介紹了解決@NonNull @org.jetbrains.annotations.NotNull報紅的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Java Swing組件單選框JRadioButton用法示例
這篇文章主要介紹了Java Swing組件單選框JRadioButton用法,結(jié)合具體實例形式分析了Swing單選框JRadioButton的使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2017-11-11idea一鍵部署SpringBoot項目jar包到服務(wù)器的實現(xiàn)
我們在開發(fā)環(huán)境部署項目一般通過idea將項目打包成jar包,然后連接linux服務(wù)器,將jar手動上傳到服務(wù)中,本文就來詳細(xì)的介紹一下步驟,感興趣的可以了解一下2023-12-12使用通過ARP類似P2P終結(jié)者實現(xiàn)數(shù)據(jù)封包
目前網(wǎng)絡(luò)上類似P2P終結(jié)者這類軟件,主要都是基于ARP欺騙實現(xiàn)的,網(wǎng)絡(luò)上到處都有關(guān)于ARP的介紹,不過為了本文讀者不需要再去查找,我就在這里大概講解一下2012-12-12