简体   繁体   English

重置倒数计时器不起作用+ Java Swing游戏中的JDialog / JOptionPane

[英]Resetting countdown timer not working + JDialog/JOptionPane in Java Swing game

I'm not that good with Java Swing and I'm trying to use a timer start the game with a delay in 3 seconds 我对Java Swing不太满意,并且尝试使用计时器延迟3秒开始游戏

But at the same time I want to show a dialog (also the game has to wait 3 seconds so the focus needs to be on the dialog) 但同时我想显示一个对话框(游戏也必须等待3秒,因此焦点必须放在对话框上)

So my dialog is as follow: got this sample code 所以我的对话框如下所示: 获得了此示例代码

So in my gameplay panel I do this: 因此,在游戏面板中,我这样做:

public class GamePlayPanel extends JPanel implements ActionListener {
    // attributes
    private JOptionCountDownTimer countDownDialog;
    public GamePlayPanel(MainWindow mainWindow) {
        // initialization attributes
        initLayoutPanel();
        this.timer = new Timer(DELAY, this);
        // Added a delay of 3 seconds so you can prepare to for the game
        this.timer.setInitialDelay(3000);
        resetTime();
    }        


    public void startGame() {
        this.gamePanel.requestFocus();
        this.countDownDialog.startCountDown();
        startTimer(); // this is my game timer to record the game time
    }


    public void restartGame() {
        this.countDownDialog.resetCountDown();
        startTimer();       
        this.gamePanel.requestFocus();
    }
}

It works fine but if I restart the game the count down timer starts at 0 -> 2 seconds. 它工作正常,但如果我重新启动游戏,则倒数计时器将从0-> 2秒开始。

Also any better ideas on my class JOptionCountDownTimer ? 在我的类JOptionCountDownTimer上还有更好的主意吗? I tried to make it extend a JDialog class but I couldn't get it to work. 我试图使其扩展JDialog类,但无法使其正常工作。

Try this out, see if it works for you. 试试看,看看是否适合您。 You can just grab the dialog class code. 您可以仅获取对话框类代码。 All you need to do is pass to it the parent frame, true for modality and seconds you want. 您需要做的就是将其传递给父框架,对于模态和所需的秒数则为true。 You also may want to pretty it up. 您可能还想修饰它。 I'm just providing the functionality 我只是提供功能

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class CountDownTimer {
    public CountDownTimer() {
        final JFrame frame = new JFrame();
        JButton button = new JButton("Open Dilaog");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                new CountDownTimerDialog(frame, true, 5);
            }
        });

        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class CountDownTimerDialog extends JDialog {
        private int count;

        public CountDownTimerDialog(JFrame parent, boolean modal, int seconds) {
            super(parent, modal);
            count = seconds;
            final JLabel countLabel = new JLabel(String.valueOf(seconds), JLabel.CENTER);
            countLabel.setFont(new Font("impact", Font.PLAIN, 36));
            JLabel message = new JLabel("Wait to Start Game");
            message.setFont(new Font("verdana", Font.BOLD, 20));

            JPanel wrapper = new JPanel(new BorderLayout());
            wrapper.setBorder(new EmptyBorder(10, 10, 10, 10));
            wrapper.add(countLabel);
            wrapper.add(message, BorderLayout.SOUTH);
            add(wrapper);

            Timer timer = new Timer(1000, new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    if (count == -1) {
                        dispose();
                    } else {
                        countLabel.setText(String.valueOf(count));
                        count--;
                    }
                }
            });
            timer.setInitialDelay(0);
            timer.start();

            pack();
            setLocationRelativeTo(parent);
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CountDownTimer();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM