用动画调整jPanels的大小 [英] Resizing jPanels with animation

查看:97
本文介绍了用动画调整jPanels的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用全屏面板开发应用程序,有时我需要在屏幕右侧显示第二个面板.

I am developing an application with a FullScreen Panel, and sometimes I need to show a second Panel, in the right side of the screen.

要开发它,我使用AbsoluteLayout(来自NetBeans)制作了一个jPanel,其中包含另外两个面板.我计算每个面板相对于屏幕分辨率的位置和大小.

To develop it I made a jPanel with AbsoluteLayout (from NetBeans) containing another two panels. I calculate each panel position and size relative to the screen resolution.

基本上,我有两个状态".首先,jPanel1的宽度为100%,而jPanel 2的宽度为0%(因此,我没有显示此面板).第二种状态是jPanel1的宽度= 75%,jPanel2的宽度= 25%.

Basically, I have two "states". In the first the jPanel1 has width = 100% and the jPanel 2 has width = 0% (So I am not showing this panel). And the second state, when jPanel1 has width = 75% and jPanel2 has width = 25%.

要进行这些转换,我只需要重新计算每个面板的大小即可,并且效果很好,但是我希望使用单个动画来完成.随着jPanel2滑入"屏幕.

To do these transitions I only recalculate each panel size and it works well, but I wish to do it with a single animation. With the jPanel2 "sliding" into the screen.

下图对此进行了解释: 链接到图像

The following image explain it: Link to image

我尝试了一些方法来实现它,但是每次尝试都以失败告终.基本上,动画"正在发生,但屏幕仅在最后刷新.在上一次尝试中,我尝试使用一个摆动计时器,我的代码是:

I tried some options to make it, but I had failed in every attempt. Basically, the "animation" is happening but the screen is refreshed only in the end. In my last attempt, I tried to use a swing Timer, my code is:

变量:

-dataPanel:主面板,具有AbsoluteLayout并包含jPanel1和jPanel2

-dataPanel: Major Panel, with AbsoluteLayout and containing jPanel1 and jPanel2

-visCons:当我在动画中更新witdh时,jPanel1位置的AbsoluteConstraints

-visCons: AbsoluteConstraints for the jPanel1 position, when I am updating the witdh in the animation

-listCons:当我在动画中更新位置时,jPanel2位置的AbsoluteConstraints.

-listCons: AbsoluteConstraints for the jPanel2 position, when I am updating the position in the animation.

public static void showListPanel() {
    timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            int width = gd.getDisplayMode().getWidth();
            int height = gd.getDisplayMode().getHeight();
            int endVis =  width - (width/4 + 20);
            for (int i = width; i >= endVis; i--) {
                visCons.width = endVis;
                listCons.x = endVis;
                dataPanel.repaint();
                dataPanel.revalidate();
                try {
                    TimeUnit.MICROSECONDS.sleep(10);
                } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            }
            timer.stop();
        }
    });
    timer.start();
}

我希望有人可以告诉我一些替代方法,或者在动画过程中刷新屏幕需要做些什么.

I hope someone can tell me some alternatives to do it, or what I need to do to refresh the screen during the animation.

推荐答案

基本上,动画"正在发生,但屏幕仅在最后刷新.

Basically, the "animation" is happening but the screen is refreshed only in the end.

当在事件调度线程上执行代码并且来自Swing侦听器的所有代码都在EDT上执行时,

不要使用sleep(..)方法.

Don't use a sleep(..) method when code executes on the Event Dispatch Thread and all code from Swing listeners do execute on the EDT.

我尝试使用摆动计时器

I tried to use a swing Timer

是的,这是正确的方法,但是您错误地使用了Timer.计时器代码中不应包含循环.那就是使用计时器的重点.您可以计划在需要动画时触发计时器.在您的情况下,您似乎希望每10ms动画一次(这可能太快了),因此您应该安排Timer每10ms触发一次.

Yes, that is the proper approach, but you are using the Timer incorrectly. You should NOT have a loop in the Timer code. That is the point of using a Timer. You schedule the Timer to fire whenever you want the animation. In your case is looks like you want the animation every 10ms (which may be too fast), so you should schedule the Timer to fire every 10ms.

由于使用的是空布局,因此不需要revalidate()(用于调用布局管理器)或重新绘制,因为更改组件的大小/位置时,repaint()会自动调用.

Since you are using a null layout there is no need for the revalidate() (which is used to invoke the layout manager) or the repaint, because when you change the size/location of a component a repaint() is automatically invoked.

这篇关于用动画调整jPanels的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆