2015-08-26 51 views
0

所以我一直在想出一个游戏的想法,而且一个需要在整个时间旅行。所以我写了一个JFrame来显示一个螺旋的.gif文件,但是当对话框出现时它不会结束,而是留在后台。我能解决这个问题吗?爪哇,结束了一个JFrame

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!"); 
} 

}

+0

你累了关闭框架?我想你可能需要某种Swing'Timer'来让动画在放入之前放置一段时间。请参阅[如何使用Swing定时器](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)以获取更多详细信息 – MadProgrammer

+0

请参见[检测/修复悬挂的紧支架代码块](http://meta.stackexchange.com/q/251795/155831)的问题,我不能再纠结于此。 –

+0

'这部分我想.gif若要end.'使用'CardLayout'或(简单)'label.setIcon(NULL);' –

回答

2

首先,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减少你确实有窗口的数量,这会为一个更好的用户体验。

+0

谢谢!我会+1你的答案,但我没有足够的代表,哈哈。完美的作品,并有我正在寻找的确切格式。 –