Swing JButton 背景色设置示例

package buttonDemo;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
 
public class CrystalButton extends JButton {  
 private String text;
    private float alpha = 1f; 
    private boolean isMouseEntered = false;
    private boolean isPressed = false;
 
   
    private Color COLOR_DARK_PURPLE = new Color(161, 123, 192);
    private Color COLOR_LIGHT_PURPLE = new Color(255, 228, 255);
    private Color COLOR_TEXT_DARKBLUE = new Color(90, 121,255);
  
    public CrystalButton(String buttonText) {  
       // super(buttonText);
        this.text = buttonText;
      
        initStyle();  
        //添加鼠标监听  
        addMouseListener(new MouseAdapter() {
         
         @Override
         public void mouseClicked(MouseEvent e){
           System.out.println("mouse click");
         }
            @Override 
            public void mouseEntered(MouseEvent e) {  
                //当鼠标进入时,鼠标进入状态改为TRUE,并重绘按钮  
                isMouseEntered = true;
                System.out.println("mouse entered");
                repaint();  
               
            }  
 
            @Override 
            public void mouseExited(MouseEvent e) {  
                isMouseEntered = false;
                System.out.println("mouse exited");
                repaint();  
            }  
           
            @Override
         public void mousePressed(MouseEvent evt)
   {
    System.out.println("mouse press");
    isPressed = true;
     repaint(); 
   }

            @Override
   public void mouseReleased(MouseEvent evt)
   {
    System.out.println("mouse release");
    isPressed = false;
     repaint(); 
   }
        });  
    }  
 
    
    private void initStyle() {  
        setOpaque(false);  
        setBorder(BorderFactory.createLineBorder(COLOR_DARK_PURPLE, 1));
        setBorderPainted(true);  
        setFocusable(false);  
    }  
 
    @Override 
    protected void paintComponent(Graphics g) {  
        super.paintComponent(g);  
        Graphics2D g2 = (Graphics2D) g;  
       
        AlphaComposite composite = AlphaComposite.getInstance(  
        AlphaComposite.SRC_OVER, alpha);  
        g2.setComposite(composite);  
        drawButtonBackground(g2, this);
    }  
 
  
    private  void drawButtonBackground(Graphics2D g2, JButton bt) {  
     if(isMouseEntered){
      if(isPressed){
       g2.setPaint(new GradientPaint(0,0, Color.WHITE, 0, bt.getHeight()*2/5, Color.WHITE));
          g2.fillRect(0, 0, bt.getWidth(), bt.getHeight()*2/5);
          g2.setPaint(new GradientPaint(0,bt.getHeight()*2/5, COLOR_LIGHT_PURPLE, 0, bt.getHeight(), Color.WHITE));
          g2.fillRect(0, bt.getHeight()*2/5, bt.getWidth(), bt.getHeight()*3/5);
      }else{       
       g2.setPaint(new GradientPaint(0,0, Color.WHITE, 0, bt.getHeight(), COLOR_LIGHT_PURPLE));
       g2.fillRect(0, 0, bt.getWidth(), bt.getHeight());
      }
     }else{
      g2.setPaint(new GradientPaint(0,0, COLOR_LIGHT_PURPLE, 0, bt.getHeight(), COLOR_DARK_PURPLE));
      g2.fillRect(0, 0, bt.getWidth(), bt.getHeight());
     }
          
      
       
        g2.setColor(COLOR_TEXT_DARKBLUE);
        g2.drawString(text,bt.getWidth()/4, this.getHeight()*3/5);
   
        g2.setClip(0,0,1000, 1000);
        g2.setPaint(new GradientPaint(0,0, COLOR_DARK_PURPLE,0 , bt.getHeight(), COLOR_DARK_PURPLE));
        g2.fillPolygon(new int[] {0,0,bt.getHeight()/2},new int[] {0,bt.getHeight(),bt.getHeight()/2},3);
       
        g2.setPaint(new GradientPaint(bt.getWidth(), bt.getHeight(), COLOR_DARK_PURPLE, bt.getWidth()-bt.getHeight()/2, bt.getHeight(), COLOR_DARK_PURPLE));
        g2.fillPolygon(new int[] {bt.getWidth(),bt.getWidth(),bt.getWidth()-bt.getHeight()/2},new int[] {0,bt.getHeight(),bt.getHeight()/2},3);
    }  
     

 

你可能感兴趣的:(button)