【问题标题】:Adding A Timer To Java Swing在 Java Swing 中添加定时器
【发布时间】:2012-07-03 11:43:17
【问题描述】:

我很抱歉在短时间内发布了多个问题,但我正试图让这个时间从 3 倒数到 0...这是我的代码

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

public class ClickingGame extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    static JFrame frame;
    static JButton startbutton, clickingbutton, timerstop;
    static JLabel timelabel, scorelabel;
    static int time = 3;
    static JTextField entertime;
    static Timer clock;
    static Timer countdown;
    static int score = 0;

    public ClickingGame() {
        setLayout(new GridLayout(3, 2, 5, 5));
        startbutton = new JButton("Start CountDown");
        timelabel = new JLabel("Time Left = NULL", SwingConstants.CENTER);
        entertime = new JTextField();
        clickingbutton = new JButton("Click Here!");
        scorelabel = new JLabel("Score = NULL", SwingConstants.CENTER);
        timerstop = new JButton("Stop Timer!");
        clock = new Timer(1000, this);
        countdown = new Timer(1000, this);

        add(entertime);
        add(startbutton);
        add(timelabel);
        add(scorelabel);
        add(clickingbutton);
        add(timerstop);
        clickingbutton.setEnabled(false);
        timerstop.setEnabled(false);
        startbutton.addActionListener(this);
        clickingbutton.addActionListener(this);
    }

    public static void openGUI() {
        frame = new JFrame("Clicking Game");
        ClickingGame contentpane = new ClickingGame();
        frame.setContentPane(contentpane);
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == startbutton) {
            startbutton.setEnabled(false);
            clickingbutton.setEnabled(true);
            time--;
            if (time > 0) {
                countdown.start();
                timelabel.setText("Starting In: " + time);
            } else {
                countdown.stop();
                time = 3;
            }
        }

        if (e.getSource() == clickingbutton) {
            score++;
            scorelabel.setText("Score = " + score);
        }
    }
}

如您所见,我的计时器“倒计时”设置正确,它只倒计时一次……我正试图让它倒计时到 3……

【问题讨论】:

标签: java swing user-interface timer


【解决方案1】:

您需要在 actionPerformed 中为您的 countdown 对象添加一个案例作为源。

编辑:

你可以试试这样的:

 if (e.getSource() == startbutton) {
        startbutton.setEnabled(false);
        clickingbutton.setEnabled(true);
        if (time > 0) {
            countdown.start();
            timelabel.setText("Starting In: " + time);
       }
    }
    if (e.getSource() == countdown){
        timelabel.setText("Starting In: " + time);

        if (time == 0) {
            countdown.stop();
            time = 3;
        } else {
            time--;
        }

    }

【讨论】:

  • 我该怎么做呢?
猜你喜欢
  • 2013-06-25
  • 2018-05-01
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 2011-03-28
  • 2014-01-18
  • 1970-01-01
  • 2013-04-24
相关资源
最近更新 更多