java - 创建多关卡游戏时如何重新启动 JFrame?

标签 java swing jframe frame restart

我创建了一个关卡的电脑游戏,我想添加另一个关卡。

这是主要内容:

public class Main extends JDialog
{   

    private static final long serialVersionUID = 1L;
    protected static TimerThread timerThread;
    static JStatusBar statusBar = new JStatusBar();
    private static JFrame frame;
    private static final int FRAME_LOCATION_X = 300;
    private static final int FRAME_LOCATION_Y = 50;
    private static final int FRAME_SIZE_X = 850; // animator's target frames per second
    private static final int FRAME_SIZE_Y = 700; // animator's target frames per second
    private static final String WorldName = "FPS 2013 CG Project";
    private static final String HARD_TARGET = "src/res/target.jpg";
    private static final String runningOut = "Time is running out - you have : ";

    static int interval;
    static Timer timer1;
    static JLabel changingLabel1 = null;


    /**
     *  NEW
     */

    private static Timer timer;
    private static int count = 60;

    private static ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            count--;
            if (count == 0)
                timer.stop();
            changingLabel1.setText(runningOut + count + " seconds"); 
        }
    };


    public static void exitProcedure() {
        timerThread.setRunning(false);
        System.exit(0);
    }


        /**
         * Clock timer1 
         * @author X2
         *
         */
        public static class TimerThread extends Thread 
        {

            protected boolean isRunning;

            protected JLabel dateLabel;
            protected JLabel timeLabel;

            protected SimpleDateFormat dateFormat = 
                    new SimpleDateFormat("EEE, d MMM yyyy");
            protected SimpleDateFormat timeFormat =
                    new SimpleDateFormat("h:mm a");

            public TimerThread(JLabel dateLabel, JLabel timeLabel) {
                this.dateLabel = dateLabel;
                this.timeLabel = timeLabel;
                this.isRunning = true;
            }

            @Override
            public void run() {
                while (isRunning) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            Calendar currentCalendar = Calendar.getInstance();
                            Date currentTime = currentCalendar.getTime();
                            dateLabel.setText(dateFormat.format(currentTime));
                            timeLabel.setText(timeFormat.format(currentTime));
                        }
                    });

                    try {
                        Thread.sleep(5000L);
                    } catch (InterruptedException e) {
                    }
                }
            }

            public void setRunning(boolean isRunning) {
                this.isRunning = isRunning;
            }

        }




    public static void main(String[] args) 
    {

           SwingUtilities.invokeLater(new Runnable() 
            {
                @Override
                public void run() 
                {

                    frame = new JFrame(WorldName);

                    Container contentPane = frame.getContentPane();
                    contentPane.setLayout(new BorderLayout());

                    /**
                     *  the timer of the count-down
                     */

                    timer = new Timer(1000, timerAction);
                    timer.start();

                    changingLabel1 = new JLabel(runningOut);
                    statusBar.setLeftComponent(changingLabel1);

                    final JLabel dateLabel = new JLabel();
                    dateLabel.setHorizontalAlignment(JLabel.CENTER);
                    statusBar.addRightComponent(dateLabel);

                    final JLabel timeLabel = new JLabel();
                    timeLabel.setHorizontalAlignment(JLabel.CENTER);
                    statusBar.addRightComponent(timeLabel);

                    contentPane.add(statusBar, BorderLayout.SOUTH);

                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                    frame.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent event) {
                            exitProcedure();
                        }
                    });

                    timerThread = new TimerThread(dateLabel, timeLabel);
                    timerThread.start();

                    Renderer myCanvas = new Renderer();
                    final Animator animator = new Animator(myCanvas);

                    Toolkit t = Toolkit.getDefaultToolkit();
                    BufferedImage originalImage = null;

                    try 
                    {
                        originalImage = ImageIO.read(new File(HARD_TARGET));
                    } 

                    catch (Exception e1) {e1.printStackTrace();}
                    Cursor newCursor = t.createCustomCursor(originalImage, new Point(0, 0), "none"); 

                    frame.setCursor(newCursor);
                    frame.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
                    frame.add(myCanvas);
                    frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
                    frame.addWindowListener(new WindowAdapter()

                    {
                        @Override
                        public void windowClosing(WindowEvent e) 
                        {
                            new Thread() 
                            {
                                 @Override
                                 public void run() 
                                 {
                                     animator.stop();
                                     System.exit(0);
                                 }
                            }.start();
                        }
                    });

                    frame.setVisible(true);
                    animator.start();
                    myCanvas.requestFocus();
                    myCanvas.setFocusable(true);
                }
            });
    }
}

这个 Main 函数使用 Renderer 类,即

class Renderer extends GLCanvas implements GLEventListener, KeyListener ,MouseListener ,MouseMotionListener {...}

该类拥有游戏的第一级。

如您所见,我也在使用 JFrameJOGL 1.0

我的问题是:完成第一级后如何重置 JFrame?显然我不能使用System.exit(0); ,因为它会退出整个程序。

我想要的是转到另一个持有第二级的类(class)。

如何在不使用 System.exit(0); 退出的情况下执行此操作?

谢谢

最佳答案

通过使用remove() ,可以有效停止面板。然后只需创建一个新的 JFrameadd() 它。考虑将 JFrame 创建作为其自己的函数,这样您就不必继续重写它。

关于java - 创建多关卡游戏时如何重新启动 JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18114622/

相关文章:

java - Jlist 和 NullPointerException

java - 如何获取 JPanel 中文本区域的位置

java - 具有组件固定宽度和高度的 GridLayout?

java - 我的 JPanel 显示为灰色

java - 为什么当我按退出键时框架不关闭?

java - 在每个 ListView 项目中添加 Heavy UI

java - 使用 deployJava.js 检测 java 版本,弹出 java 升级菜单

java - 将 jar 库注入(inject) apk 应用程序

模拟 Swing 静态方法时的 java.lang.VerifyError

java - 无法在 JPanel 上扩展类以在 JFrame 类中显示