单击空格时启动的Java计时器,再次单击空格后结束

时间:2014-01-11 02:51:19

标签: java swing timer countdown key-bindings

我正在尝试编写一个Rubiks立方体计时器。一旦你点击空格我希望计时器从15秒开始倒计时,一旦15秒结束,从0开始向上计数。当你完成解决方案时你再次点击空间,停止计时器(我希望计时器计数到最近的百分之一。这就是我现在所拥有的:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class CubeMain extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CubeMain frame = new CubeMain();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public CubeMain() {

        setTitle("Cube Timer");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 490);
        contentPane = new JPanel();
        final JLabel Timerlbl = new JLabel("");
        Timerlbl.setBounds(269, 219, 46, 14);
        contentPane.add(Timerlbl);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                Timerlbl.setText("Label Change");

            }
        });
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

    }
}

1 个答案:

答案 0 :(得分:2)

所以你知道你需要某种计时器。您想要Swing程序的计时器是javax.swing.Timer。这是基本的构造函数

Timer(int delay, ActionListener listener);

其中,delay是触发操作之间的延迟时间,并且侦听器侦听每个间隔触发的那些计时器操作事件。一个基本的实现我会像这样

public TimerPanel() {
    Timer timer = new Timer(1000, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            // do something
        }
    });
    timer.start();
}

你可以做的是有一个count变量,你可以使用该变量来设置timerLabel。然后,只需将SPACE的键绑定设置为timer.start()timer.stop()

看看这个,它有计时器。这几乎是你需要的东西

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class TimerPanel{

    double count = 15.00;
    boolean reverse = true;
    boolean started = false;

    private JLabel timerLabel = new JLabel(String.format("%.2f", count));
    private Timer timer;

    public TimerPanel() {
        timerLabel.setHorizontalAlignment(JLabel.CENTER);
        timerLabel.setBorder(new EmptyBorder(20, 20, 20, 20));

        JFrame frame = new JFrame();
        frame.add(timerLabel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);


        timer = new Timer(10, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (reverse && count > 0) {
                    count -= 0.01;
                    timerLabel.setText(String.format("%.2f", count));
                    if (count <= 0) {
                        reverse = false;
                    }
                }
                if (!reverse){
                    count += 0.01;
                    timerLabel.setText(String.format("%.2f", count));
                }
            }
        });

        Action spaceAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if (!started) {
                    timer.start();
                    started = true;
                } else {
                    timer.stop();
                    count = 15.00;
                    started = false;
                    reverse = true;
                }
            }
        };

        InputMap inputMap = timerLabel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = timerLabel.getActionMap();

        inputMap.put(KeyStroke.getKeyStroke("SPACE"), "spaceAction");
        actionMap.put("spaceAction", spaceAction);
    }


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