【问题标题】:Solution for countdown timer for my swing game. (cardlayout)我的挥杆游戏倒数计时器的解决方案。 (卡片布局)
【发布时间】:2014-03-11 17:24:28
【问题描述】:

我正在制作一个秋千迷宫游戏,我使用卡片布局在我的游戏中从一个面板转到另一个面板(主菜单 --> 选择难度屏幕 --> 游戏屏幕)。
我已经设置好我的游戏和菜单,现在我正在尝试添加一个倒计时时钟,让用户知道他在游戏结束前还剩 X 秒。


我做了一个倒数计时器,它适用于 1 个地图(1 个面板),但当我尝试将它用于其他面板(我的其他 2 个地图)时。它变得非常有问题。计时器在达到 0 后不会停止执行其操作(选项窗格和哔声)。 因此,当我将 TimerEvent 和 TimerClass 类链接到我的 GUI 时(我在此处发布的其他 gui,但要在此处发布多行),TimerEvent 和 TimerClass 类确实有效。但是当我尝试向其他面板添加更多计时器时(对于我的游戏之间的不同级别),它会变得很糟糕。 对不起我的英语不好,非常感谢你!

真诚的,初学者程序员

MENU 类(我为您制作的非常简短的基本版本供您测试):

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

    }
}
}

【问题讨论】:

    标签: java swing events timer layout-manager


    【解决方案1】:

    为什么不这样做:

    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){
        }
    }
    

    【讨论】:

    • 我在 webz 上读到我必须小心使用线程,最好使用摇摆计时器:s 但我会试试你的,如果它有效..那么它有效。我会告诉你的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多