我现在有一个问题 - 当我用我的自定义按钮调用 frame.setState(Frame.ICONIFIED)
时(我没有使用默认的 JFrame 最小化按钮 - JFrame 设置为 setUndecorated( true)
),JFrame 只是在没有任何动画的情况下转到任务栏。在正常情况下,它应该逐渐去任务栏最小化自己。但是,如果我在任务栏上按 iconfied JFrame,它会通过动画恢复到正常大小。这种情况是在 Windows XP 上进行的,没有在其他系统上测试过,但我想它的行为方式是一样的。
最佳答案
由于您希望它独立于平台,因此我的其他答案不适合您。此外,当您最小化或隐藏窗口时,每个平台都会有不同的 react 。您提到,如果能看到能为您提供一致动画的 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 来找到它,我只是让动画淡出窗口而不将其缩小到任务栏可能的位置em> 是。
希望这对您有所帮助!
关于java - JFrame 过渡效果 - 当调用 setState(Frame.ICONIFIED) 时,它只是转到任务栏而没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17761134/