Swing计时器和较差的性能(Java、Swing)

Swing计时器和较差的性能(Java、Swing),java,swing,Java,Swing,我有一个关于摆动计时器的问题。我试图为按下按钮时从侧面滑入的JPanel创建一个“滑动”动画。这实际上工作得很好,但当我想让它再次滑出时,我会被极其糟糕的性能所打击。注意:JPanel位于JLayeredPane中,它下面还有其他组件(这很重要) 以下是滑入的代码: int boundsTo = GlobalValue.getLargeInformationBarWidth(); final int[] boundsNow = {0}; inSlideTimer

我有一个关于摆动计时器的问题。我试图为按下按钮时从侧面滑入的JPanel创建一个“滑动”动画。这实际上工作得很好,但当我想让它再次滑出时,我会被极其糟糕的性能所打击。注意:JPanel位于JLayeredPane中,它下面还有其他组件(这很重要)

以下是滑入的代码:

int boundsTo = GlobalValue.getLargeInformationBarWidth();
        final int[] boundsNow = {0};
        inSlideTimer = new Timer(1, ae -> {
            if (boundsTo > boundsNow[0]){
                boundsNow[0] = boundsNow[0] + 3;
                JourneyPlannerBarController.getInstance().getInformationBar().setBounds(0, 0, boundsNow[0], window.getFrame().getHeight());
                JourneyPlannerBarController.getInstance().getInformationBar().revalidate();
                JourneyPlannerBarController.getInstance().getInformationBar().repaint();
            } else {
                inSlideTimer.stop();
                inSlideTimer = null;
            }
        });
        inSlideTimer.start();
        JourneyPlannerBarController.getInstance().setupLargeJourneyPlannerBar();
}
下面是幻灯片的代码:

int boundsTo = 0;
final int[] boundsNow = {GlobalValue.getLargeInformationBarWidth()};
inSlideTimer = new Timer(1, ae -> {
        if (boundsTo <= boundsNow[0]) {
            boundsNow[0] = boundsNow[0] - 3;
            JourneyPlannerBarController.getInstance().getInformationBar().setBounds(0, 0, boundsNow[0], window.getFrame().getHeight());
            JourneyPlannerBarController.getInstance().getInformationBar().revalidate();
            JourneyPlannerBarController.getInstance().getInformationBar().repaint();

        } else {
            inSlideTimer.stop();
            inSlideTimer = null;
            JourneyPlannerBarController.getInstance().clearJourneyPlannerBar();
            CanvasController.repaintCanvas();
        }
});
inSlideTimer.start();
}
int-boundsTo=0;
final int[]boundsNow={GlobalValue.getLargeInformation BarWidth()};
inSlideTimer=新计时器(1,ae->{

如果(尝试增加延迟到5-16i之间)也会考虑“时间”。基于解决方案,动画将运行一段固定的时间,它通常会产生一个整体更好的解决方案感谢回复!我已经尝试增加延迟,tbh,似乎没有任何差异,一直到100。这就像EDT被阻塞,但我不知道为什么,因为它是ALE正好与幻灯片相反,它工作得很好。至于你建议的基于时间的解决方案,你能再详细说明吗?记住,计时器是在EDT中触发的。也许考虑发布一个可运行的例子是我们需要能够帮助的。侧写“时间”基于解决方案,动画将运行一段固定的时间,它通常会产生一个整体更好的解决方案感谢回复!我已经尝试增加延迟,tbh,似乎没有任何差异,一直到100。这就像EDT被阻塞,但我不知道为什么,因为它是盟友正好与幻灯片相反,它工作得很好。至于你建议的基于时间的解决方案,你能再详细说明吗?记住,计时器是在EDT中触发的。也许考虑发布一个可运行的例子是我们需要能够帮助的事情。