Java,结束一个 JFrame [英] Java, ending a JFrame

查看:29
本文介绍了Java,结束一个 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在构思一款游戏的创意,而一款游戏需要穿越时空.所以我写了一个 JFrame 来显示一个螺旋的 .gif,但它没有在对话框出现时结束,而是留在背景中.我可以解决这个问题吗?

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

}

推荐答案

首先,将 defaultCloseOperation 更改为类似 DO_NOTHING_ON_CLOSE 的内容,这样至少可以防止用户关闭当您自己处理框架时,请关闭窗口并阻止 JVM 退出.

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.

接下来,经过短暂的延迟(因为效果真的很酷),您想在框架上调用 dispose 以关闭它.

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

为了实现这一点,您可以使用 Swing Timer,例如...

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

查看如何使用 Swing Timers更多详情.

您还应该考虑使用 CardLayout 减少您实际拥有的窗口数量,这将有助于获得更好的用户体验.

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

这篇关于Java,结束一个 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆