我的秋千游戏倒计时的解决方案(卡片布局)

5fjcxozz  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(231)

我正在做一个摇摆迷宫游戏,我使用卡片布局在我的游戏中从一个面板转到另一个面板(主菜单-->选择难度屏幕-->游戏屏幕)。
我已经设置了游戏和菜单,现在我正在尝试添加一个倒计时时钟,让用户知道他在游戏结束前还有x秒。
我做了一个倒计时,它可以完美地为1个Map(1个面板),但当我试图使用它为其他面板(我的其他2个Map)。它真的很烦人。计时器在达到0(选项窗格和哔哔声)后不会停止执行其操作。因此,当我将timerevent和timerclass类链接到我的gui(我在这里发布的其他gui,但在这里发布的行太多)时,它们就可以工作了。但是当我尝试在其他面板上添加更多的计时器时(在我的游戏之间的不同级别),它会变得很麻烦。对不起,我的英语不好,非常感谢你!!
真诚的,一个初学者程序员

菜单类(非常简短和基本的版本,我为你做的测试):

package labyrinthproject.View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Card {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JPanel panelCont = new JPanel();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JLabel timerLabel = new JLabel();
    JButton gopanel1 = new JButton("go panel1");
    JButton gopanel2 = new JButton("go panel2");
    final CardLayout cl = new CardLayout();
    panelCont.setLayout(cl);

    frame.setPreferredSize(new Dimension(800,600));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panelCont);
    frame.pack();

    panel1.setPreferredSize(new Dimension(800,600));
    panel1.setBackground(Color.red);
    panel1.add(gopanel1);
    panel1.add(gopanel2);
    gopanel1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "2");
        }
    });
    gopanel2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(panelCont, "3");
        }
    });
    panel2.setPreferredSize(new Dimension(800,600));
    panel2.setBackground(Color.black);
    panel2.add(timerLabel)
    panel3.setPreferredSize(new Dimension(800,600));
    panel3.setBackground(Color.blue);

    panelCont.add(panel1, "1");
    panelCont.add(panel2, "2");
    panelCont.add(panel3, "3");
    frame.setVisible(true);

}

}

我的timerevent课程(稍微改变了一下,看起来适合上面的课程)

public class TimerEvent implements ActionListener {
static TimerEvent e = new TimerEvent();
    public void actionPerformed(ActionEvent e){
        int count = 60;
        Card.timerLabel.setText("Time: " + count);

        TimerClass tc = new TimerClass(count);
        Card.timer = new Timer(1000, tc);
        Card.timer.start();
    }
}

我的timerclass:

public class TimerClass implements ActionListener {

int counter;

public TimerClass(int counter) {
    this.counter = counter;

}

public void actionPerformed(ActionEvent tc) {
    counter--;

    if (counter >= 1) {
        Card.timerLabel.setText("Time:" + counter);
    } else {
        Card.timer.stop();
        Card.timerLabel.setText("Done");
        Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(MainMenu.panelCont, "Time expired, game over");
        Card.cl.show(MainMenu.panelCont, "2");
        Card.timer.stop();

    }
}
}
pgky5nke

pgky5nke1#

为什么不这样做:

int counter;
for (int j = 0 ; j >=counter ; j++) {
   Card.timerLabel.setText("Time:" + counter);
   delayer (1000);
}

private static void delayer (long milliseconds)
{
    try {
        Thread.sleep (1000);
    }

    catch (InterruptedException ex){
    }
}

相关问题