寻找一种更好的方法让JLabel淡入/淡出

时间:2014-03-12 15:27:01

标签: java user-interface jlabel fadein alpha

现在我有了这个(只是增加了标签的alpha,中间有小的断点):

            ae.getErrorLabel().setVisible(true);
            ae.getErrorLabel().setForeground(new Color(255, 0, 0, 0));
            ae.getErrorLabel().setBackground(new Color(0, 0, 0, 0));
            int alpha = 0;
            while (alpha < 255) {
                // wait-Methode
                long time0, time1;
                time0 = System.currentTimeMillis();
                do {
                    time1 = System.currentTimeMillis();
                } while ((time1 - time0) < 0.1 * 1000);
                // =====================================
                alpha += 25;
                if (alpha > 255)
                    alpha = 255;
                ae.getErrorLabel().setForeground(
                        new Color(255, 0, 0, alpha));
                ae.getErrorLabel().setBackground(
                        new Color(128, 128, 128, alpha));

这使得JLabel“淡入” 但它看起来有点儿麻烦,看起来不太好看。
有没有更好的方法(这不是太复杂)?

顺便说一句:它是swing,一个简单的JLabel on smiple JPanel,我在一个单独的线程中使用这个代码,当你按下按钮时运行

1 个答案:

答案 0 :(得分:2)

正确实现动画并不容易。出于这个原因,使用经过测试的库会更好。其中最好的是Trident。以下是相关链接:

https://kenai.com/projects/trident/pages/Home

https://github.com/kirillcool/trident