带 alpha 值的 Jlabel
可能的重复:
如何在 swing 中淡入淡出图像?
我有 jLabel我想每秒更改其不透明度(alpha 值),我尝试了类似的操作,但它不会每秒更改,JLabel 仅使用最后一个 alpha 值更改其不透明度。
Color color = jLabel1.getBackground();
int alpha = 255;
long initTime = System.currentTimeMillis();
while(true){
if(System.currentTimeMillis() - initTime >= 1000){
initTime = System.currentTimeMillis();
alpha -=1;
Color color2 = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
jLabel1.setBackground(color2);
}
if(alpha<=0)
break;
}
Possible Duplicate:
How do I fade an image in swing?
i have jLabel and i want to change its opacity (alpha value) each one second , i tried something like that but its not change each one second , JLabel change its opacity only with last alpha value .
Color color = jLabel1.getBackground();
int alpha = 255;
long initTime = System.currentTimeMillis();
while(true){
if(System.currentTimeMillis() - initTime >= 1000){
initTime = System.currentTimeMillis();
alpha -=1;
Color color2 = new Color(color.getRed(),color.getGreen(),color.getBlue(),alpha);
jLabel1.setBackground(color2);
}
if(alpha<=0)
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在事件调度线程上使用
SwingUtilities.invokeLater
运行此操作,则重绘只会在您的代码执行完毕后才会发生。对于重复更新,请使用 Swing 计时器,如本 sun 教程中所述:您还可以查看 Trident Swing 动画库。
If you're running this on the Event Dispatch Thread, using say
SwingUtilities.invokeLater
then the repaint will only happen after your code has finished executing. For repeated updates, use the Swing Timer, as detailed in this sun tutorial:You might also look into the Trident animation library for Swing.