如何创建显示/隐藏详细信息按钮?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 javax.swing.Timer,而不是java.util.Timer...或使用 Trident 。
Use javax.swing.Timer, not java.util.Timer... or use Trident.
在 TimerTask 的 run() 方法中设置大小后尝试调用 myDialog.repaint() 。
Trying calling
myDialog.repaint()
after setting the size in the TimerTask'srun()
method.