码迷,mamicode.com
首页 > 其他好文 > 详细

JButton变换样式

时间:2018-06-16 10:30:17      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:log   public   rap   alpha   new   dap   uid   nta   str   

 

JButton变换样式

摘自:绘制JButton圆角效果 http://caleb-520.iteye.com/blog/1039493

 

RButton btnNewButton_1 = new RButton("New button");
btnNewButton_1.setPreferredSize(new Dimension(107, 50));

 

 

 

 

package com.example.test;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JButton;

public class RButton extends JButton {
    private static final long serialVersionUID = 39082560987930759L;
//    public static final Color BUTTON_COLOR1 = new Color(255, 255, 255);
//    public static final Color BUTTON_COLOR2 = new Color(255, 255, 255);
    public static final Color BUTTON_COLOR1 = new Color(205, 255, 205);
    public static final Color BUTTON_COLOR2 = new Color(51, 154, 47);
//     public static final Color BUTTON_COLOR1 = new Color(125, 161, 237);
//     public static final Color BUTTON_COLOR2 = new Color(91, 118, 173);
    public static final Color BUTTON_FOREGROUND_COLOR = Color.WHITE;
    private boolean hover;

    public RButton(String name) {
        this.setText(name);
        setFont(new Font("system", Font.PLAIN, 12));
        setBorderPainted(false);
        setForeground(BUTTON_COLOR2);
        setFocusPainted(false);
        setContentAreaFilled(false);
        addMouseListener(new MouseAdapter() {
/*            @Override
            public void mouseEntered(MouseEvent e) {
//                setForeground(BUTTON_FOREGROUND_COLOR);
                hover = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
//                setForeground(BUTTON_COLOR2);
                hover = false;
                repaint();
            }*/
            
            @Override
            public void mousePressed(MouseEvent e){
                setForeground(BUTTON_FOREGROUND_COLOR);
                hover = true;
                repaint();
            } 
            
            @Override
            public void mouseReleased(MouseEvent e){
                setForeground(BUTTON_COLOR2);
                hover = false;
                repaint();
            } 
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        int h = getHeight();
        int w = getWidth();
        float tran = 1F;
        if (!hover) {
            tran = 0.3F;
        }

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint p1;
        GradientPaint p2;
        if (getModel().isPressed()) {
            p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, h - 1,
                    new Color(100, 100, 100));
            p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, h - 3,
                    new Color(255, 255, 255, 100));
        } else {
            p1 = new GradientPaint(0, 0, new Color(100, 100, 100), 0, h - 1,
                    new Color(0, 0, 0));
            p2 = new GradientPaint(0, 1, new Color(255, 255, 255, 100), 0,
                    h - 3, new Color(0, 0, 0, 50));
        }
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                tran));
        RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1,
                h - 1, 10, 10);
        Shape clip = g2d.getClip();
        g2d.clip(r2d);
        GradientPaint gp = new GradientPaint(0.0F, 0.0F, BUTTON_COLOR1, 0.0F,
                h, BUTTON_COLOR2, true);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        g2d.setClip(clip);
        g2d.setPaint(p1);
        g2d.drawRoundRect(0, 0, w - 1, h - 1, 10, 10);
        g2d.setPaint(p2);
        g2d.drawRoundRect(1, 1, w - 3, h - 3, 9, 9);
        g2d.dispose();
        super.paintComponent(g);
    }
}

 

JButton变换样式

标签:log   public   rap   alpha   new   dap   uid   nta   str   

原文地址:https://www.cnblogs.com/LiuYanYGZ/p/9189710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!