You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing实现JFrame高度调整的平滑过渡动画技术咨询

JFrame展开/收起平滑过渡动画实现方案

核心思路

你当前的代码是一次性修改窗口高度,所以没有过渡效果。Swing中必须借助javax.swing.Timer实现平滑动画——它在EDT(事件调度线程)中定时触发任务,逐帧调整窗口尺寸,避免线程安全问题,这和Python GUI中用定时器做动画的逻辑类似。

修改后完整代码

import javax.swing.Timer;
// 其他必要导入...

// 类内成员变量
private static final int ANIMATION_STEP = 5; // 每次调整的高度步长
private static final int TARGET_EXPANDED_HEIGHT = 450; // 展开后目标高度
private static final int TARGET_COLLAPSED_HEIGHT = 370; // 收起后目标高度
private Timer animationTimer;

// 初始化定时器(放在构造方法或初始化代码块中)
private void initAnimationTimer() {
    animationTimer = new Timer(10, e -> {
        int currentHeight = frame.getHeight();
        if (!heightIncreased[0]) {
            // 执行展开动画
            if (currentHeight < TARGET_EXPANDED_HEIGHT) {
                frame.setSize(frame.getWidth(), currentHeight + ANIMATION_STEP);
            } else {
                // 动画结束,完成面板添加与状态更新
                frame.add(bottomFrame, BorderLayout.PAGE_END);
                changeBottomFrame.setText("∨");
                heightIncreased[0] = true;
                animationTimer.stop();
            }
        } else {
            // 执行收起动画
            if (currentHeight > TARGET_COLLAPSED_HEIGHT) {
                frame.setSize(frame.getWidth(), currentHeight - ANIMATION_STEP);
            } else {
                // 动画结束,完成面板移除与状态更新
                frame.remove(bottomFrame);
                changeBottomFrame.setText("∧");
                heightIncreased[0] = false;
                animationTimer.stop();
            }
        }
        frame.validate();
        frame.repaint();
    });
}

// 按钮点击事件处理
changeBottomFrame.addActionListener(e -> {
    // 防止动画未完成时重复触发
    if (!animationTimer.isRunning()) {
        initAnimationTimer();
        animationTimer.start();
    }
});

关键细节说明

  • 定时器参数:第一个参数是帧间隔(单位:毫秒),10ms对应约100帧/秒,数值越小动画越流畅;第二个参数是每帧执行的逻辑。
  • 步长设置:ANIMATION_STEP控制每次高度调整的幅度,值越小过渡越平滑,可根据需求调整。
  • 组件操作时机:仅在动画完全结束后执行add/remove组件操作,避免界面闪烁或布局混乱。
  • 线程安全:javax.swing.Timer的任务在EDT中执行,符合Swing的线程规则,不会出现界面卡顿或异常。

内容的提问来源于stack exchange,提问作者jes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 04:06:02