私が時間をかけて取り組んだのは、停止してリセットするまでの時間を計測するストップウォッチやクロノメーターのように、ユーザーがスタート ボタンをクリックしてからの経過時間または残り時間を表示するプログラムです。経過時間を測定する他の例としては、レーシング ゲームのラップ タイムや、他のゲームのミリ秒単位の時間制限があります。
ただし、自分のストップウォッチが実際の時間と同じ速度で実行されていないため、問題が発生しています。タイマーが 1 秒上下するのに 1 秒以上かかります。
JLabel
コードは次のとおりです: (GUI は完全に機能します。1 秒ごとに に表示される時間が 1 秒少なくなるように、経過時間を表示する値を制御する方法についてもっと心配しています。変更できません。Thread.sleep
タイマーをさらに悪化させるため、渡される引数。)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.concurrent.*;
public class StopwatchGUI3 extends JFrame
{
private static final long serialVersionUID = 3545053785228009472L;
// GUI Components
private JPanel panel;
private JLabel timeLabel;
private JPanel buttonPanel;
private JButton startButton;
private JButton resetButton;
private JButton stopButton;
// Properties of Program.
private byte centiseconds = 0;
private byte seconds = 30;
private short minutes = 0;
private Runnable timeTask;
private Runnable incrementTimeTask;
private Runnable setTimeTask;
private DecimalFormat timeFormatter;
private boolean timerIsRunning = true;
private ExecutorService executor = Executors.newCachedThreadPool();
public StopwatchGUI3()
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
timeLabel = new JLabel();
timeLabel.setFont(new Font("Consolas", Font.PLAIN, 13));
timeLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(timeLabel);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
startButton = new JButton("Start");
startButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (!timerIsRunning)
timerIsRunning = true;
executor.execute(timeTask);
}
});
buttonPanel.add(startButton);
resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
timerIsRunning = false;
centiseconds = 0;
seconds = 30;
minutes = 0;
timeLabel.setText(timeFormatter.format(minutes) + ":"
+ timeFormatter.format(seconds) + "."
+ timeFormatter.format(centiseconds));
}
});
buttonPanel.add(resetButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
timerIsRunning = false;
}
});
buttonPanel.add(stopButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
timeFormatter = new DecimalFormat("00");
timeTask = new Runnable(){
public void run()
{
while(timerIsRunning)
{
executor.execute(incrementTimeTask);
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
};
incrementTimeTask = new Runnable(){
public void run()
{
if (centiseconds > 0)
centiseconds--;
else
{
if (seconds == 0 && minutes == 0)
timerIsRunning = false;
else if (seconds > 0)
{
seconds--;
centiseconds = 99;
}
else if (minutes > 0)
{
minutes--;
seconds = 59;
centiseconds = 99;
}
}
executor.execute(setTimeTask);
}
};
setTimeTask = new Runnable(){
public void run()
{
timeLabel.setText(timeFormatter.format(minutes) + ":"
+ timeFormatter.format(seconds) + "."
+ timeFormatter.format(centiseconds));
}
};
timeLabel.setText(timeFormatter.format(minutes) + ":"
+ timeFormatter.format(seconds) + "."
+ timeFormatter.format(centiseconds));
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("StopwatchGUI.java");
pack();
setVisible(true);
}
public static void main(String[] args)
{
new StopwatchGUI3();
}
}
3 つの別々のスレッドに依存する代わりに、本物のストップウォッチのように、タイマーをリアルタイムで同期させる別の方法が必要です。これは、このような大規模なプログラミング プロジェクトには多すぎると思いますが、入門レベルでは問題ありません。今。(ちなみに、DecimalFormat
クラスは実際のストップウォッチのように数値を適切にフォーマットすることですが、四捨五入する小数値はありません。私がこれを投稿した時点で、 というテキストクラスが存在するのは今だけSimpleDateFormat
です。)
つまり、このプログラムを単なるストップウォッチにしたいのです。そうでない場合、たとえば Java ゲームでストップウォッチを作成または使用するにはどうすればよいでしょうか。