开发者

Jlabel with alpha value [duplicate]

This question already has answers here: Closed 10 years ago.

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.开发者_C百科setBackground(color2);

        }
        if(alpha<=0)
            break;
    }


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:

  • How to use Swing Timers

You might also look into the Trident animation library for Swing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜