首先,defaultCloseOperation
更改为类似DO_NOTHING_ON_CLOSE
,这将至少阻止关闭窗口的用户,并从当你处理帧自己已经退出停止JVM。
接下来,在短暂的延迟之后(因为效果是真的很酷),你要拨打的框架dispose
以关闭它。
为了达到这个目的,你可以使用一个Swing Timer
,例如...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Game {
public static void main(String[] args) {
new Game();
}
public Game() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
try {
URL url = new URL("https://s-media-cache-ak0.pinimg.com/originals/e8/e4/02/e8e4028941eb06f2fd5c10f44bfc5e1b.gif");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Trippy");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
Timer timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();
//This is the part i want the .gif to end. Instead, it just runs in the background.
JOptionPane.showMessageDialog(null, "You have traveled through space and time!");
}
});
timer.setRepeats(false);
timer.start();
} catch (MalformedURLException exp) {
exp.printStackTrace();
}
}
});
}
}
看一看How to use Swing Timers了解更多详情。
你也应该考虑使用CardLayout
减少你确实有窗口的数量,这会为一个更好的用户体验。
你累了关闭框架?我想你可能需要某种Swing'Timer'来让动画在放入之前放置一段时间。请参阅[如何使用Swing定时器](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)以获取更多详细信息 – MadProgrammer
请参见[检测/修复悬挂的紧支架代码块](http://meta.stackexchange.com/q/251795/155831)的问题,我不能再纠结于此。 –
'这部分我想.gif若要end.'使用'CardLayout'或(简单)'label.setIcon(NULL);' –