如何创建显示/隐藏详细信息按钮?

发布于 2024-09-07 07:08:41 字数 972 浏览 2 评论 0原文

我正在使用 Java Swing 创建一个 JDialog,并且我正在尝试创建一个显示/隐藏详细信息按钮来显示/隐藏此 JDialog 底部的报告。

它对我来说效果很好,但我想花时间做到这一点,在显示/隐藏报告时添加一个小动画效果,我使用了 TimerTask 但它只是直接显示报告,没有任何慢动作...这是我当前的代码:

private void showHideDetailsButtonActionPerformed() {
    final MyDialog myDialog = this;
    int fullHeight = this.getHeight();
    int smallHeight = this.getHeight()/2 - 4;
    this.setSize( this.getWidth(), smallHeight );  // By default hide the report.

    if( this.getHeight() == smallHeight ) {  // Show details.
        new Timer().schedule(
            new java.util.TimerTask() {
                @Override
                public void run() {
                    while( myDialog.getHeight() < fullHeight ) {
                        myDialog.setSize( myDialog.getWidth(), myDialog.getHeight() + 1 );
                        System.out.println( myDialog.getHeight() );
                    }
                }
            }, 
            800
        );
    }
}

I am using Java Swing to create a JDialog, and i am trying to create a Show/Hide details button to show/hide a report at the bottom of this JDialog.

It works fine for me, but i want to do this with time, to add a small animation effect while showing/hiding the report, i have used TimerTask but it's just showing the report directly without any slow motion ... Here's my current code :

private void showHideDetailsButtonActionPerformed() {
    final MyDialog myDialog = this;
    int fullHeight = this.getHeight();
    int smallHeight = this.getHeight()/2 - 4;
    this.setSize( this.getWidth(), smallHeight );  // By default hide the report.

    if( this.getHeight() == smallHeight ) {  // Show details.
        new Timer().schedule(
            new java.util.TimerTask() {
                @Override
                public void run() {
                    while( myDialog.getHeight() < fullHeight ) {
                        myDialog.setSize( myDialog.getWidth(), myDialog.getHeight() + 1 );
                        System.out.println( myDialog.getHeight() );
                    }
                }
            }, 
            800
        );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-09-14 07:08:41

使用 javax.swing.Timer,而不是java.util.Timer...或使用 Trident

Use javax.swing.Timer, not java.util.Timer... or use Trident.

清醇 2024-09-14 07:08:41

在 TimerTask 的 run() 方法中设置大小后尝试调用 myDialog.repaint() 。

Trying calling myDialog.repaint() after setting the size in the TimerTask's run() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文