繁体   English   中英

JFrame过渡效果 - 当调用setState(Frame.ICONIFIED)时,它只会进入没有动画的任务栏

[英]JFrame transition effect - when called setState(Frame.ICONIFIED) it just goes to Taskbar without animation

我现在有一个问题 - 当我用自定义按钮调用frame.setState(Frame.ICONIFIED)时(我没有使用默认的JFrame最小化按钮 - JFrame设置为setUndecorated(true) ),JFrame只是进入任务栏而没有任何动画。 在正常情况下,它应该逐渐转到任务栏最小化自己。 但是如果我在任务栏上按下iconfied JFrame,它会将动画恢复到正常大小。 情况是在Windows XP上,没有在其他系统上测试过,但我认为它的行为方式相同。

由于您希望它与平台无关,因此我的其他答案对您不起作用。 此外,当您最小化或隐藏窗口时,每个平台都会有不同的反应。 你提到看到一个可以为你提供一致动画的Java代码片段会很高兴。 这是给你的一些代码。

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;

public class FadeUtilityClass
{
    private static final int TIME = 200;
    private static final int MILLIS_PER_FRAME = 33;
    private static final float DELTA = MILLIS_PER_FRAME / (float)TIME; //how much the opacity will change on each tick

    /**
     * @param frame the frame to fade in or out
     * @param in true if you are fading in, false if you're fading out
     */
    public static void fade(final JFrame frame, final boolean in)
    {
        frame.setOpacity(in ? 0f : 1f); //if we're fading in, make sure our opacity is 0, and 1 if we're fading out
        if (in) //set the state back to normal because we might have been minimized
            frame.setState(JFrame.NORMAL); 
        final Timer timer = new Timer();
        TimerTask timerTask = new TimerTask()
        {
            float opacity = in ? 0f : 1f;
            float delta = in ? DELTA : -DELTA;

            @Override
            public void run()
            {
                opacity += delta; //tweak the opacity
                if (opacity < 0) //we're invisible now
                {
                    frame.setState(JFrame.ICONIFIED); //hide frame
                    frame.setOpacity(1f); //then make it opaque again, so it'll reappear properly if they click the taskbar 
                    timer.cancel(); //stop the timer
                }
                else if (opacity > 1) //we're fully visible now
                {
                    frame.setOpacity(1f); //make the opacity an even 1.0f
                    timer.cancel(); //stop the timer
                }
                else
                    frame.setOpacity(opacity);
            }
        };
        timer.scheduleAtFixedRate(timerTask, MILLIS_PER_FRAME, MILLIS_PER_FRAME);
    }
}

它是一个实用工具类,可以使未装饰的框架淡入或淡出。 由于任务栏的位置和最小化的窗口基于平台而变化,你需要使用特定于平台的api来找到它,我只是让动画淡出窗口而不将其缩小到任务栏可能的位置。

希望这可以帮助!

如果您所说的是真的,那么您未修饰的窗口在单击任务栏中的图标时实际上会生成动画。 然后,您可以使用JNA在代码中触发相同的操作。

要使此解决方案正常工作,您需要在类路径中包含jna.jarjna -platform.jar

这不是JNA教程,周围有很多。 这只是使用JNA调用user32.dll函数CloseWindowOpenIcon的代码 ; 这些是Windows在您单击应用程序的托盘图标时调用的功能 - (事实上​​,我不确定这些是实际功能,但它们的反应相同)

import java.awt.Component;
import java.awt.Window;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;

public class IconifyUtilityClass
{
    public static void minimize(Window window)
    {
        HWND hWnd = getHWND(window);
        User32dll.INSTANCE.CloseWindow(hWnd); //call CloseWindow with this windows handle
    }

    public static void restore(Window window)
    {
        HWND hWnd = getHWND(window);
        User32dll.INSTANCE.OpenIcon(hWnd); //call OpenIcon with this windows handle
    }

    private interface User32dll extends Library
    {
        User32dll INSTANCE = (User32dll) Native.loadLibrary("user32.dll", User32dll.class);
        boolean OpenIcon(HWND hWnd);
        boolean CloseWindow(HWND hWnd);
    }

    private static HWND getHWND(Component comp)
    {
        return new HWND(Native.getComponentPointer(comp));
    }
}

要调用代码,只需传入框架或对话框即可

JFrame frame = new JFrame();
...
IconifyUtilityClass.minimize(frame); //minimize your frame
...
IconifyUtilityClass.restore(frame); //restore your frame

值得注意的是,在Windows 7中,未装饰的帧根本没有动画(即使使用user32.dll AnimateWindow函数)。 因此,如果你的目标是让你的帧在任何地方都有动画,那么你是对的,你必须自己做。 JavaFx有一些非常好的动画内容可以帮助解决这个问题。

通过电话

myJFrame.setUndecorated( true )

您正在为JFrame禁用任何框架装饰。 这包括最大化和最小化JFrame的动画。

如果您跳过此呼叫或将其更改为

myJFrame.setUndecorated( false )

然后动画将再次可见,即使通过调用最小化帧

myJFrame.setState( Frame.ICONIFIED );

AFAIK,没有办法为未修饰的框架制作动画。 :(

请在此处找到我的完整代码示例:

    JFrame frame = new JFrame();
    frame.setSize( 800, 600 );
    frame.setUndecorated( false );  // will enable animations
    frame.setVisible( true );
    try { Thread.sleep( 3000 ); } catch ( Throwable t ) {}
    frame.setState( Frame.ICONIFIED );

问候

克里斯托弗

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM