溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)Swing組件定制Button示例

發(fā)布時(shí)間:2020-09-02 12:19:31 來源:腳本之家 閱讀:173 作者:fearlessMore 欄目:編程語言

本文實(shí)例講述了Java實(shí)現(xiàn)Swing組件定制Button。分享給大家供大家參考,具體如下:

先來看看運(yùn)行效果圖:

Java實(shí)現(xiàn)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);
   }
  });
 }
}

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI