Ali Sepehry
Ali Sepehry

Reputation: 3

Java, ending a JFrame

So I've been thinking up ideas for a game, and one requires traveling throughout time. So i wrote a JFrame to display a .gif of a spiral, but instead of it ending when the dialog shows up, it stays in the background. Can i fix this?

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

}

Upvotes: 0

Views: 176

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

First, change the defaultCloseOperation to something like DO_NOTHING_ON_CLOSE, this will at least prevent the user from closing the window and stop the JVM from been exited when you do dispose of the frame yourself.

Next, after a short delay (because the effect is really cool), you want to call dispose on the frame in order to close it.

In order to achieve this, you could use a Swing Timer, for example...

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();
                }
            }
        });
    }
}

Have a look at How to use Swing Timers for more details.

You should also consider using a CardLayout to reduce the number of windows you actually have, this will make for a better user experience.

Upvotes: 2

Related Questions