是否javax.swing.Timer timer;
足以计算经过的时间?我正在做一个俄罗斯方块项目,它还计算玩家玩游戏所花费的时间。在play
方法中,
public void play() {
reset();
onPlay = true;
Thread thread = new Thread(new Game());
thread.start();
rightPanel.setPlayButtonEnable(false);
rightPanel.setPause(true);
rightPanel.setStop(true);
playItem.setEnabled(false);
pauseItem.setEnabled(true);
stopItem.setEnabled(true);
setBlockColorItem.setEnabled(false);
setBGColorItem.setEnabled(false);
rightPanel.requestFocus();
//time.start();
//timeElapsed = System.currentTimeMillis();
RightPanel.timeElapsedTextField.setText("");
timer.start();
startTime = System.currentTimeMillis();
}
在stop
方法中,
public void stop() {
isStop=true;
onPlay = false;
if (piece != null)
piece.isMoving = false;
playItem.setEnabled(true);
rightPanel.setPlayButtonEnable(true);
pauseItem.setEnabled(false);
rightPanel.setPauseButtonLabel(true);
stopItem.setEnabled(false);
resumeItem.setEnabled(false);
setBlockColorItem.setEnabled(true);
setBGColorItem.setEnabled(true);
rightPanel.setPause(false);
rightPanel.setStop(false);
reset();
//time.stop();
timer.stop();
long delta = (System.currentTimeMillis() - startTime)/10;
RightPanel.timeElapsedTextField.setText(Double.toString(delta/100.0) + " seconds");
}
这些够吗?每当尝试显示经过的时间时,JTextField 什么都不显示。有什么问题?非常感谢!
public void run() {
int column = 4, style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
//timer.start();
//startTime = System.currentTimeMillis();
while (onPlay) {
if (piece != null) {
if (piece.isAlive()) {
try {
Thread.currentThread().sleep(100);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine();
if (isGameOver()) {
playItem.setEnabled(true);
pauseItem.setEnabled(true);
resumeItem.setEnabled(false);
rightPanel.setPlayButtonEnable(true);
rightPanel.setPauseButtonLabel(true);
displayGameOver();
return;
}
piece = new Piece(style, -1, column, board);
piece.start();
//numDropped = numDropped+1;
//RightPanel.scoreTextField.setText(numDropped+"");
style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
rightPanel.setTipStyle(style);
numDropped = numDropped+1;
RightPanel.numDroppedTextField.setText(numDropped+"");
//RightPanel.numDroppedTextField = new JTextField(numDropped+"");
long startTime = System.currentTimeMillis();
}
long estimatedTime = ((System.currentTimeMillis() - startTime)/1000);
RightPanel.timeElapsedTextField.setText(estimatedTime+"");
}