目录
题目:*16.20(累计秒表)
习题思路
代码示例
结果展示
题目:*16.20(累计秒表)
编写一个程序,模拟一个秒表,如图16-45a所示。当用户单击Start按钮时,按钮的标签变为Pause,如图16-45b所示。当用户单击Pause按钮时,按钮的标签变为Resume,如图16-45c所示。Clear按钮重设计数为0并且重设按钮的标签为Start。
-
习题思路
- 创建一个文本Text和两个按钮Button,用三个Long类型的参数表示开始时间、结束时间、总时长。
- 创建一个HBox,把两个按钮放进来,再创建一个BorderPane,将Text设置在中心,将HBox设置在底部。
- 为第一个按钮注册事件监听器,可以用一个int或者Boolean表示秒表当前的状态,从而设置按钮上的文本。
- 为第二个按钮注册事件监听器,当事件触发时停止动画,将总时长参数设置为0。
- 分别设置开始计时方法、暂停计时方法、重新开始计时方法,在第一个按钮的事件监听器上根据秒表当前状态调用。
- 创建一个Timeline对象来循环将时间设置到Text上
-
代码示例
编程练习题16_20AccumulatedStopwatch.java
package chapter_16;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class 编程练习题16_20AccumulatedStopwatch extends Application{
private Timeline timerAnimation;
private int Control = 0;//0为停止(Start),1为进行中(Pause),2为暂停(Resume)
private Button btControl = new Button("Start");
private Button btClear = new Button("Clear");
private Text text = new Text("00:00:00");
private long startTime;
private long endTime;
private long allTime = 0;
@Override
public void start(Stage primaryStage) throws Exception {
text.setFont(new Font("华文细黑", 48));
HBox hBox = new HBox(10);
hBox.setPadding(new Insets(5, 5, 5, 5));
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(btControl,btClear);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(text);
borderPane.setBottom(hBox);
btControl.setOnMouseClicked(e ->{
if(btControl.getText().equals("Start"))
Control = 0;
else if(btControl.getText().equals("Pause"))
Control = 1;
else if(btControl.getText().equals("Resume"))
Control = 2;
setBtText(Control);
setText();
});
btClear.setOnMouseClicked(e->{
pause();
allTime = 0;
text.setText("00:00:00");
});
EventHandler<ActionEvent> eventHandler = e -> {
if(Control == 0||Control == 1) {
long currentTime = System.currentTimeMillis();
allTime += currentTime - startTime;
startTime = currentTime;
setText();
}
};
timerAnimation = new Timeline(new KeyFrame(Duration.millis(100), eventHandler));
timerAnimation.setCycleCount(Timeline.INDEFINITE);
Scene scene = new Scene(borderPane,350, 150);
primaryStage.setTitle("编程练习题16_20AccumulatedStopwatch");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void setBtText(int control) {
switch (control) {
case 0: btControl.setText("Pause");start();break;
case 1: btControl.setText("Resume");pause();break;
case 2: btControl.setText("Pause");resume();break;
}
}
public void start() {
Control = 1;
startTime = System.currentTimeMillis();
timerAnimation.play();
}
public void pause() {
Control = 2;
endTime = System.currentTimeMillis();
allTime += endTime - startTime;
timerAnimation.stop();
}
public void resume() {
Control = 0;
start();
}
public void setText() {
long seconds = allTime/1000%60;
long minutes = allTime/1000/60%60;
long hours = allTime/1000/60/60%24;
text.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
-
结果展示