Java语言程序设计基础篇_编程练习题*16.20(累计秒表)

目录

题目:*16.20(累计秒表)

习题思路

代码示例

结果展示


题目:*16.20(累计秒表)

编写一个程序,模拟一个秒表,如图16-45a所示。当用户单击Start按钮时,按钮的标签变为Pause,如图16-45b所示。当用户单击Pause按钮时,按钮的标签变为Resume,如图16-45c所示。Clear按钮重设计数为0并且重设按钮的标签为Start。

习题思路
  1. 创建一个文本Text和两个按钮Button,用三个Long类型的参数表示开始时间、结束时间、总时长。
  2. 创建一个HBox,把两个按钮放进来,再创建一个BorderPane,将Text设置在中心,将HBox设置在底部。
  3. 为第一个按钮注册事件监听器,可以用一个int或者Boolean表示秒表当前的状态,从而设置按钮上的文本。
  4. 为第二个按钮注册事件监听器,当事件触发时停止动画,将总时长参数设置为0。
  5. 分别设置开始计时方法、暂停计时方法、重新开始计时方法,在第一个按钮的事件监听器上根据秒表当前状态调用。
  6. 创建一个Timeline对象来循环将时间设置到Text上
代码示例

编程练习题16_20AccumulatedStopwatch.java

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));  
	}
}
结果展示
相关推荐
小白小白从不日白4 分钟前
react 组件通讯
前端·react.js
陈大爷(有低保)8 分钟前
UDP Socket聊天室(Java)
java·网络协议·udp
c4fx13 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
Redstone Monstrosity21 分钟前
字节二面
前端·面试
kinlon.liu21 分钟前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
李小星同志23 分钟前
高级算法设计与分析 学习笔记6 B树
笔记·学习
东方翱翔28 分钟前
CSS的三种基本选择器
前端·css
霜晨月c34 分钟前
MFC 使用细节
笔记·学习·mfc
鸽芷咕37 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
王哲晓42 分钟前
Linux通过yum安装Docker
java·linux·docker