java 透明jlabel_Java中在JLabel中添加了图片如何在图片上面加一层透明层

满意答案

00e27ab806e4881f8254fe7ae8741834.png

kpbn3217

2015.08.12

00e27ab806e4881f8254fe7ae8741834.png

采纳率:44%    等级:9

已帮助:566人

直接设置一个透明图片的JLabel呗

我正好有个透明标签类,你试试看123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Composite;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Transparency; import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;  public class LimpidLabel extends JLabel {         public static void main(String[] args) {        JFrame frm = new JFrame("demo");        LimpidLabel lbl = new LimpidLabel("1.png", 0.5f);        frm.add(lbl);        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frm.pack();        frm.setLocationRelativeTo(null);        frm.setVisible(true);           }         public LimpidLabel(String imagePath, float alpha) {        this(new ImageIcon(imagePath).getImage(), alpha);    }         public LimpidLabel(Image img, float alpha) {        int w, h;        try        {            w = img.getWidth(this);               //读取图片长度            h = img.getHeight(this);          //读取图片宽度                         GraphicsConfiguration gc = new JFrame().getGraphicsConfiguration();        // 本地图形设备             Image image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);        //建立透明画布            Graphics2D g = (Graphics2D) image.getGraphics();                           //在画布上创建画笔                         Composite a = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha); //指定透明度为alpha            g.setComposite(a);            g.drawImage(img, 0, 0, this);           //将image画到g画笔所在的画布上            g.dispose();                           //释放内存             this.setIgnoreRepaint(true);            //不自动重绘            this.setFocusable(false);               //设置没有焦点            this.setIcon(new ImageIcon(image));   //把刚才生成的半透明image设置为Icon        } catch (Exception e) {            e.printStackTrace();        }    } }

00分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值