本文實例講述了Java實現Swing組件定制Button。分享給大家供大家參考,具體如下:
創(chuàng)新互聯建站是一家專注于做網站、網站建設與策劃設計,丹江口網站建設哪家好?創(chuàng)新互聯建站做網站,專注于網站建設10多年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:丹江口等地區(qū)。丹江口做網站價格咨詢:18980820575先來看看運行效果圖:
具體代碼如下:
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.JButton; 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 ButtonSkinDemo { 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("Button")) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } JFrame frame = new JFrame("www.jb51.net - Button 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 buttonDefaults = new UIDefaults(); buttonDefaults.put("Button.backgroundPainter", 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.fillRoundRect(0, 0, w - 2, h - 2, 8, 8); g.setColor(Color.LIGHT_GRAY); g.drawRoundRect(0, 0, w - 2, h - 2, 8, 8); } }); buttonDefaults.put("Button[Pressed].backgroundPainter", 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.LIGHT_GRAY); g.fillRoundRect(0, 0, w - 2, h - 2, 8, 8); } }); JButton button = new JButton("myButton"); panel.add(button); button.putClientProperty("Nimbus.Overrides", buttonDefaults); button.putClientProperty("Nimbus.Overrides.InheritDefaults", false); // Add a normal themed slider for comparison JButton normalButton = new JButton("normalButton"); panel.add(normalButton); frame.getContentPane().add(panel, BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
當前文章:Java實現Swing組件定制Button示例-創(chuàng)新互聯
標題網址:http://m.newbst.com/article20/jjcco.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站建設、網站制作、網站導航、小程序開發(fā)、電子商務、靜態(tài)網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯