我正在编写一个应用程序,该应用程序执行某些任务并在成功完成任务时通知用户。为了通知用户我正在使用 jlabel。我希望这个 jlabel 显示消息并在一段时间后消失。我使用 netbeans 作为我的 IDE。
这是我的课程的架构。
摘要,GUI代码
abstract class Admin extends JFrame{
protected static jlabel lbl_message= new jlabel("some text");
// other functions and variables
abstarct protected void performButtonClickAction();
}
用于实现抽象函数并提供其他功能的类。
final class AdminActionPerformer extends Admin{
final public void performButtonClickAction(){
// code for doin the task
if(task is successful){
new Thread(new Fader(Admin.lbl_message)).start();
}
}
public static void main(String[] args) {
new AdminActionPerformer().setVisible(true);
}
}
用于制作 Jlabel 后退的线程
class Fader implements Runnable{
javax.swing.JLabel label;
Color c;
Fader(javax.swing.JLabel label){
this.label=label;
c=label.getBackground();
}
public void run() {
int alpha=label.getGraphics().getColor().getAlpha()-5;
while(alpha>0){
System.out.println(alpha);
alpha-=25;
label.getGraphics().setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha));
label.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(Fader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
但标签并没有消失。我在这里做错了什么?谢谢 :)
附:我已将 JLabel opaque 设置为 true。那是问题吗?我想最初显示标签及其背景颜色,然后使其消失。
如果您要做很多效果,您可能会使用摇摆动画库,例如Trident http://kenai.com/projects/trident/pages/Home。以下是淡出标签的方法。
JLabel label = ....;
Timeline timeline = new Timeline(label);
timeline.addPropertyToInterpolate("background", label.getBackground(),
new Color(label.getBackground().getRGB(), true));
timeline.addPropertyToInterpolate("foreground", label.getForeground(),
new Color(label.getForeground().getRGB(), true));
timeline.play();
编辑:更新以更改前景色/背景色,这是标签上的属性。但是,对于生产代码,我会将标签包装在一个可以将 alpha 应用于其子级的容器中,然后您可以以组件中立的方式淡入/淡出组件。其中一个动画套件中有一个用于此目的的组件,但我现在找不到它......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)