我有JFrame
上显示的计时器,但我的问题是我无法将计时器格式化为正确。计时器正在画布上显示,所以我使用graphics2D来显示它。
有没有快速的格式化方法,还是我必须放一堆if语句?
private Graphics2D graphics;
private Timer timer = new Timer(1000, new TimerListener());
private int minutes = 0, seconds = 60;
// Draws the timer.
public void drawTimer() {
graphics.setColor(Color.WHITE);
graphics.setFont(new Font("Georgia", Font.PLAIN, 26));
graphics.drawString(minutes + ":" + seconds, getWidth() / 2, 35);
}
// Part of the timer.
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
seconds--;
if (seconds == -1) {
seconds = 60;
}
}
}
当我想要启动计时器时,我只需要打电话:
timer.start();
我希望它看起来像:
1时, 0:59, 0:58, 等