Java,结束JFrame
所以我一直在想一个游戏的点子,一个游戏需要穿越时空。所以我写了一个JFrame来显示一个.gif的螺旋图,但是它并没有在对话框出现时结束,而是停留在背景中。我能修好这个吗Java,结束JFrame,java,swing,Java,Swing,所以我一直在想一个游戏的点子,一个游戏需要穿越时空。所以我写了一个JFrame来显示一个.gif的螺旋图,但是它并没有在对话框出现时结束,而是停留在背景中。我能修好这个吗 import java.awt.*; import java.net.*; import javax.swing.*; public class Game { public static void main(String[] args) throws MalformedURLException { URL url
import java.awt.*;
import java.net.*;
import javax.swing.*;
public class Game {
public static void main(String[] args) throws MalformedURLException {
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.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
//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!");
}
}首先,将
defaultCloseOperation
更改为类似于DO\u NOTHING\u ON\u CLOSE
,这至少可以防止用户关闭窗口,并在您自己处理帧时阻止JVM退出
接下来,在短暂的延迟之后(因为效果非常酷),您需要调用帧上的dispose
,以关闭它
为了实现这一点,您可以使用Swing计时器,例如
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();
}
}
});
}
}
请查看以了解更多详细信息
你也应该考虑使用A来减少你实际上拥有的窗口数量,这将有助于更好的用户体验。
你是否已经厌倦了关闭框架?我认为您可能需要某种类型的Swing计时器,以便在关闭动画之前留出时间放置动画。查看更多详细信息查看一个我再也懒得解决的问题。这是我希望.gif结束的部分。
使用卡片布局
或(更简单)标签.setIcon(空)代码>谢谢!我会+1你的答案,但我没有足够的代表,哈哈。工程完美,并有确切的格式,我正在寻找。