让 JLabel 消失 [英] Making a JLabel Fade away

查看:25
本文介绍了让 JLabel 消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序来完成一些任务并在任务成功完成后通知用户.通知用户我正在使用 jlabel.我希望这个 jlabel 显示消息并在一段时间后消失.我使用 netbeans 作为我的 IDE.

I'm writing an application that do some task and inform user upon successful completion of the task. To inform user I am using a jlabel. I want this jlabel to display the message and fadeaway after a while. I am using netbeans as my IDE.

这是我的类的架构.

抽象,图形界面代码

abstract class Admin extends JFrame{
  protected static jlabel lbl_message= new jlabel("some text");
  // other functions and variables

  abstarct protected void performButtonClickAction();

}

用于实现抽象函数和提供其他功能的类.

Class for implementing abstract functions and providing other functionalities.

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 消失的线程

Thread for making the Jlabel fadeaway

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);
        }
     }
  }
}

但标签不会消失.我在这里做错了什么?谢谢:)

But the label does not fadeaway. What am I doing wrong here? Thank you :)

附言我已将 JLabel opaque 设置为 true.那是问题吗?我想首先显示带有背景颜色的标签,然后让它逐渐消失.

P.S. I have set JLabel opaque true. Is that a problem? I want to display the label with its background colour initially and then make it fade away.

推荐答案

我同意 Johannes 的观点,即从不同的 Thread 更新您的 UI 可能效果不佳.有像 SwingWorkerSwingUtilities.invokeLater 这样的机制可以解决你的错误线程问题.

I agree with Johannes that updating your UI from a different Thread is likely not to work well. There are mechanisms like SwingWorker and SwingUtilities.invokeLater that might solve your wrong-thread problems.

不过,我发现更大的问题是您正在与 JLabel 争夺谁来绘制它.JLabel 是一个普通的组件,每当它的容器被刷新时,它就会以其通常的颜色重新绘制自己.这是一个谁画得最后,谁就赢了"的案例.

The bigger problem I see, though, is that you're fighting with your JLabel over who paints it. The JLabel is a normal component, and whenever its container is refreshed it repaints itself, in its usual colors. This is a case of "whoever paints last, wins."

替代建议:为什么不只是摆弄标签的颜色属性?如果您使前景色接近背景色,则会看到它正在褪色.当您更改颜色时,标签将自行重新绘制,或者您可以使用 update()repaint() 强制重新绘制,我一直忘记是哪个.

Alternative suggestion: Why not just fiddle with the label's color attribute? If you make the foreground color approach the background color, it will be seen to be fading. The label will either repaint itself when you change its color, or you can force a repaint using update() or repaint(), I keep forgetting which.

编辑

我想我明白了这不起作用的真正原因.您正在设置标签的图形上下文的颜色.这没有任何影响!任何绘制自身部分的组件都会在绘制任何东西之前立即设置颜色,即将其刷子浸入墨水中.当然,它为绘制字符字形而设置的颜色是原始标签颜色.只有当标签绘制代码愚蠢地在绘制前忘记设置颜色时,您的方法才有效.

I think I see the real reason this doesn't work. You're setting the label's graphics context's color. This has no effect! Any component that paints parts of itself will set the color, i.e. dip its brush in ink, immediately before drawing anything. And of course the color it sets up for drawing its character glyphs is the original label color. Your method would only work if the label painting code foolishly forgot to set the color before drawing.

这篇关于让 JLabel 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆