Java實現Swing組件定製Button樣本,swingbutton

來源:互聯網
上載者:User

Java實現Swing組件定製Button樣本,swingbutton

本文執行個體講述了Java實現Swing組件定製Button。分享給大家供大家參考,具體如下:

先來看看運行:

具體代碼如下:

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);   }  }); }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.