Java 如何使JPanel变得越来越透明,最终消失?
我想制作一个扩展Java 如何使JPanel变得越来越透明,最终消失?,java,swing,jpanel,transparency,Java,Swing,Jpanel,Transparency,我想制作一个扩展JPanel的面板,当它变得可见时,它开始变得越来越透明,最终消失。我的代码有什么问题 public class BaloonPanel extends JPanel { private float transparency = 1f; Timer timer; public BaloonPanel() { setBackground(Color.white); ActionListener action = new ActionListener()
JPanel
的面板,当它变得可见时,它开始变得越来越透明,最终消失。我的代码有什么问题
public class BaloonPanel extends JPanel
{
private float transparency = 1f;
Timer timer;
public BaloonPanel()
{
setBackground(Color.white);
ActionListener action = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
transparency = transparency - 0.01f;
if (transparency < 0.0f)
{
timer.stop();
}
repaint();
}
};
timer = new Timer(100, action);
timer.start();
}
@Override
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
super.paint(g2);
g2.dispose();
}
公共类阳台面板扩展JPanel
{
私人浮动透明度=1f;
定时器;
公共阳台小组()
{
挫折地面(颜色:白色);
ActionListener动作=新建ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
透明度=透明度-0.01f;
如果(透明度<0.0f)
{
timer.stop();
}
重新油漆();
}
};
定时器=新定时器(100,动作);
timer.start();
}
@凌驾
公共空间涂料(图g)
{
Graphics2D g2=(Graphics2D)g.create();
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,transparency));
超级油漆(g2);
g2.dispose();
}
}因为
BallonPanel
不透明,因此重绘管理器不必费心在其下方进行绘制。这是一个优化的油漆过程,为什么油漆不需要油漆
您需要“说服”重绘管理器在仍然绘制组件背景的情况下在组件下方进行绘制
public class FadePane {
public static void main(String[] args) {
new FadePane();
}
public FadePane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.BLUE);
frame.setBackground(Color.BLUE);
frame.add(new BaloonPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BaloonPanel extends JPanel {
private float transparency = 1f;
Timer timer;
public BaloonPanel() {
setBackground(Color.white);
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
transparency = transparency - 0.1f;
if (transparency < 0.1f) {
transparency = 0;
timer.stop();
}
invalidate();
repaint();
}
};
timer = new Timer(100, action);
timer.setRepeats(true);
setOpaque(false);
final JButton fade = new JButton("Fade");
fade.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
fade.setEnabled(false);
}
});
setLayout(new GridBagLayout());
add(fade);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
System.out.println(transparency);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
super.paint(g2);
g2.dispose();
}
}
}
将BallonPanel
设置为透明(setOpaque(false)
)并更新paint
方法以填充背景
public class FadePane {
public static void main(String[] args) {
new FadePane();
}
public FadePane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.BLUE);
frame.setBackground(Color.BLUE);
frame.add(new BaloonPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BaloonPanel extends JPanel {
private float transparency = 1f;
Timer timer;
public BaloonPanel() {
setBackground(Color.white);
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
transparency = transparency - 0.1f;
if (transparency < 0.1f) {
transparency = 0;
timer.stop();
}
invalidate();
repaint();
}
};
timer = new Timer(100, action);
timer.setRepeats(true);
setOpaque(false);
final JButton fade = new JButton("Fade");
fade.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timer.start();
fade.setEnabled(false);
}
});
setLayout(new GridBagLayout());
add(fade);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
System.out.println(transparency);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
super.paint(g2);
g2.dispose();
}
}
}
公共类FadePane{
公共静态void main(字符串[]args){
新法地潘();
}
公共法迪潘(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(例外情况除外){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(颜色为.BLUE);
框架。背景(颜色。蓝色);
frame.add(新的阳台面板());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类阳台面板扩展JPanel{
私人浮动透明度=1f;
定时器;
公共阳台小组(){
挫折地面(颜色:白色);
ActionListener动作=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
透明度=透明度-0.1f;
如果(透明度<0.1f){
透明度=0;
timer.stop();
}
使无效();
重新油漆();
}
};
定时器=新定时器(100,动作);
timer.setRepeats(真);
设置不透明(假);
最终JButton褪色=新JButton(“褪色”);
fade.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
timer.start();
fade.setEnabled(假);
}
});
setLayout(新的GridBagLayout());
添加(褪色);
}
@凌驾
公共空间涂料(图g){
Graphics2D g2=(Graphics2D)g.create();
System.out.println(透明度);
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,transparency));
g2.setColor(getBackground());
g2.fillRect(0,0,getWidth(),getHeight());
超级油漆(g2);
g2.dispose();
}
}
}
您希望代码做什么?你的代码实际上是做什么的?描述两者之间的不匹配。“帮助我们帮助你”。@rossum正如我上面所描述的,我希望我的面板变得更透明,更透明,最终消失。我的代码没有继续到我想要的点,它导致面板变得更加透明(仅)…+1表示AlphaComposite
;看laso,这会改变饱和度。