天天看点

java 半透明颜色_java 的按钮半透明化

这个是我的代码importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTestextendsJFrame{publicTest(){MyPanelmy=newMyPanel();add(my);}publicstaticvoidmain(S...

这个是我的代码

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Test extends JFrame {

public Test()

{

MyPanel my = new MyPanel();

add(my);

}

public static void main(String[] args)

{

JFrame f=new Test();

f.setSize(500, 500);

f.setVisible(true);

}

}

class MyPanel extends JPanel implements MouseListener {

private MyLabel l1;

public MyPanel()

{

setLayout(null);

}

protected void paintComponent(Graphics g) {

Image im = Toolkit.getDefaultToolkit().getImage(“back.jpg");//背景图

g.drawImage(im, 0, 0, this);

l1 = new MyLabel(new Point(90, 10));

l1.addMouseListener(this);

add(l1);

}

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {

l1.setbk();//关键!!不是在Point(90,10) 处添加调整Label的background

}

public void mouseExited(MouseEvent e) {

repaint();

}

}

class MyLabel extends JLabel {

private Point p;

public MyLabel(Point point) {

super("");

p = point;

setBounds(p.x, p.y, 50, 50);

setBackground(new Color(0, 0, 0, 50));

setOpaque(true);

}

public void setbk() {

setBackground(new Color(0, 0, 0, 150));

}

}

我把鼠标放进透明label后,想要获得当前label所在透明度更低的图像块,可是得到的是基于原点的像素块,求助!!谢谢啊

展开