选中小时 分钟 秒
代码附上:
package com.example.javafx03;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.io.IOException;
import com.example.javafx03.HelloController;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("hello-view.fxml"));
Parent root = loader.load();
stage.setTitle("计时器");
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("/css/main.css").toExternalForm());
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
--------------------------------------------------------------------------------------
package com.example.javafx03;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.util.Duration;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
public class HelloController implements Initializable {
private int second;
private int minute;
private int hour;
private Timeline timeline;
private enum TimeSelect { HOUR , MINUTE , SECOND }
private TimeSelect selected = TimeSelect.SECOND;
private MediaPlayer mediaPlayer;
@FXML
private Button addButton;
@FXML
private Button endButton;
@FXML
private Text hourLabel;
@FXML
private Text minuteLabel;
@FXML
private Text secondLabel;
@FXML
private Button reduceButton;
@FXML
private Button startButton;
@FXML
private Button stopButton;
@FXML
private Rectangle hourRectangle;
@FXML
private Rectangle minuteRectangle;
@FXML
private Rectangle secondRectangle;
@FXML
void start(MouseEvent event) {
initialize();
// 如果计时器已经在运行,直接返回,避免重复启动
if (timeline != null && timeline.getStatus() == Animation.Status.RUNNING) {
return;
}
if (hour == 0 && minute == 0 && second == 0){
return;
}
// 创建一个Timeline对象,用于在指定的时间间隔内执行动画
timeline = new Timeline(new KeyFrame(Duration.seconds(1), e ->{
if (!decrementTime()){
timeline.stop();
Platform.runLater(() -> showAlert("计时结束", "时间到了!"));
alertSound();
}
updateLabel();
}
));
//代码设置了Timeline的循环次数为无限。这意味着动画会一直重复,直到它被停止或场景关闭。
timeline.setCycleCount(Timeline.INDEFINITE);
// 启动动画
timeline.play();
}
public void initialize() {
hour = Integer.parseInt(hourLabel.getText());
minute = Integer.parseInt(minuteLabel.getText());
second = Integer.parseInt(secondLabel.getText());
}
public boolean decrementTime(){
if (second > 0){
second --;
}else if (minute >0){
minute --;
second = 59;
}else if(hour > 0){
hour --;
minute = 59;
second = 59;
}else {
//已经结束
timeline.stop();
return false;
}
//还未结束
return true;
}
// 显示警报方法
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
//当用户关闭弹窗执行操作
alert.setOnHidden(event -> stopAlertSound());
//阻塞当前线程,直到用户关闭警报窗口
alert.showAndWait();
}
public void alertSound(){
// 创建一个Media对象,指定音频文件的路径
Media media = new Media(getClass().getResource("/alert/11301.mp3").toExternalForm());
// 创建一个MediaPlayer对象来控制音频播放
mediaPlayer = new MediaPlayer(media);
// 播放声音
mediaPlayer.play();
}
public void stopAlertSound(){
// 停止音频播放
mediaPlayer.stop();
}
public void updateLabel(){
hourLabel.setText(String.format("%02d", hour));
minuteLabel.setText(String.format("%02d", minute));
secondLabel.setText(String.format("%02d", second));
}
@FXML
void add(MouseEvent event) {
switch (selected){
case HOUR -> updateTime(1, hourLabel, 24);
case MINUTE -> updateTime(1, minuteLabel, 60);
case SECOND -> updateTime(1, secondLabel,60);
}
}
@FXML
void reduce(MouseEvent event) {
switch (selected){
case HOUR -> updateTime(-1, hourLabel, 24);
case MINUTE -> updateTime(-1, minuteLabel, 60);
case SECOND -> updateTime(-1, secondLabel,60);
}
}
@FXML
void stop(MouseEvent event) {
timeline.stop();
}
@FXML
void end(MouseEvent event) {
timeline.stop();
hourLabel.setText("00");
minuteLabel.setText("00");
secondLabel.setText("00");
}
//更新时间方法
public void updateTime(Integer increment, Text label,Integer max){
if (timeline != null && timeline.getStatus() == Animation.Status.RUNNING) {
return;
}
int time = Integer.parseInt(label.getText());
int value = (time + increment) % max;
if (value >= 0){
label.setText(String.format("%02d", value));
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
hourRectangle.setOnMouseClicked(event -> {
selected = TimeSelect.HOUR;
});
minuteRectangle.setOnMouseClicked(event -> {
selected = TimeSelect.MINUTE;
});
secondRectangle.setOnMouseClicked(event -> {
selected = TimeSelect.SECOND;
});
}
}
---------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="@../../../css/main.css" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.javafx03.HelloController">
<stylesheets>
<URL value="@/css/main.css" /> <!-- 引用CSS文件 -->
</stylesheets>
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#968d8d00" height="209.0" layoutX="55.0" layoutY="45.0" opacity="0.14" stroke="BLACK" strokeType="INSIDE" style="-fx-arc-height: 30; -fx-arc-width: 30;" width="525.0" />
<Text fx:id="hourLabel" layoutX="85.0" layoutY="177.0" stroke="#dfdfdf" strokeType="INSIDE" text="00" wrappingWidth="106.60003662109375">
<font>
<Font name="System Bold Italic" size="73.0" />
</font>
</Text>
<Button fx:id="startButton" layoutX="103.0" layoutY="301.0" mnemonicParsing="false" onm ouseClicked="#start" opacity="0.9" style="-fx-background-radius: 20;-fx-background-image: url('Images/start.png'); -fx-background-size: 50% 50%; -fx-background-repeat: no-repeat; -fx-background-position: center;" />
<Button fx:id="stopButton" layoutX="273.0" layoutY="301.0" mnemonicParsing="false" onm ouseClicked="#stop" style="-fx-background-radius: 20;-fx-background-image: url('Images/stop.png'); -fx-background-size: 50% 50%; -fx-background-repeat: no-repeat; -fx-background-position: center;" />
<Button fx:id="addButton" layoutX="189.0" layoutY="301.0" mnemonicParsing="false" onm ouseClicked="#add" style="-fx-background-radius: 20; -fx-background-image: url('Images/add.png'); -fx-background-size: 50% 50%; -fx-background-repeat: no-repeat; -fx-background-position: center; ">
<font>
<Font size="11.0" />
</font>
</Button>
<Button fx:id="reduceButton" layoutX="359.0" layoutY="301.0" mnemonicParsing="false" onm ouseClicked="#reduce" style="-fx-background-radius: 20;-fx-background-image: url('/Images/reduce.png'); -fx-background-size: 50% 50%; -fx-background-repeat: no-repeat; -fx-background-position: center;" />
<Text fx:id="minuteLabel" layoutX="246.0" layoutY="180.0" stroke="#796c63" strokeType="OUTSIDE" strokeWidth="0.0" text="00" wrappingWidth="106.60003662109375">
<font>
<Font name="System Bold" size="73.0" />
</font>
</Text>
<Text fx:id="secondLabel" layoutX="421.0" layoutY="180.0" strokeType="OUTSIDE" strokeWidth="0.0" text="00" wrappingWidth="106.60003662109375">
<font>
<Font name="System Bold Italic" size="73.0" />
</font>
</Text>
<Text layoutX="192.0" layoutY="171.0" strokeType="OUTSIDE" strokeWidth="0.0" text=":" wrappingWidth="47.4000244140625">
<font>
<Font size="73.0" />
</font>
</Text>
<Text layoutX="351.0" layoutY="171.0" strokeType="OUTSIDE" strokeWidth="0.0" text=":" wrappingWidth="47.4000244140625">
<font>
<Font size="73.0" />
</font>
</Text>
<Button fx:id="endButton" layoutX="448.0" layoutY="301.0" mnemonicParsing="false" onm ouseClicked="#end" style="-fx-background-radius: 20;-fx-background-image: url('Images/end.png'); -fx-background-size: 50% 50%; -fx-background-repeat: no-repeat; -fx-background-position: center;" />
<Rectangle fx:id="secondRectangle" arcHeight="5.0" arcWidth="5.0" fill="#818a91" height="165.0" layoutX="413.0" layoutY="61.0" opacity="0.0" stroke="BLACK" strokeType="INSIDE" style="-fx-arc-height: 30; -fx-arc-width: 30;" styleClass="hoverEffect" width="117.0" />
<Rectangle fx:id="hourRectangle" arcHeight="5.0" arcWidth="5.0" fill="#818a91" height="165.0" layoutX="68.0" layoutY="61.0" opacity="0.0" stroke="BLACK" strokeType="INSIDE" style="-fx-arc-height: 30; -fx-arc-width: 30;" styleClass="hoverEffect" width="123.0" />
<Rectangle fx:id="minuteRectangle" arcHeight="5.0" arcWidth="5.0" fill="#818a91" height="165.0" layoutX="229.0" layoutY="61.0" opacity="0.0" stroke="BLACK" strokeType="INSIDE" style="-fx-arc-height: 30; -fx-arc-width: 30;" styleClass="hoverEffect" width="117.0" />
</children>
</AnchorPane>
软件已经打包成exe,感兴趣下载
标签:定时器,javafx,void,Javafx,scene,private,FXML,开发,import From: https://blog.csdn.net/weixin_67996964/article/details/143649724