-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmClockGUI.java
154 lines (126 loc) · 5.29 KB
/
AlarmClockGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.example.alarmclock;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Timer;
import java.util.TimerTask;
public class AlarmClockGUI extends Application {
private final Timer timer;
private LocalDateTime alarmDateTime;
private boolean alarmTriggered;
private Label timeLabel;
private Label dateLabel;
private TextField hourField;
private TextField minuteField;
private DatePicker datePicker;
private Button setAlarmButton;
public AlarmClockGUI() {
timer = new Timer();
alarmDateTime = null;
alarmTriggered = false;
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Relógio");
// Cria os componentes da interface
dateLabel = new Label();
dateLabel.setStyle("-fx-font-size: 36px;");
timeLabel = new Label();
timeLabel.setStyle("-fx-font-size: 64px;");
Label hourLabel = new Label("Horas: ");
hourField = new TextField();
hourField.setMaxWidth(60);
Label minuteLabel = new Label("Minutos: ");
minuteField = new TextField();
minuteField.setMaxWidth(60);
Label dateLabel2 = new Label("Data:");
datePicker = new DatePicker();
datePicker.setMaxWidth(120);
datePicker.setValue(LocalDate.now());
setAlarmButton = new Button("Acionar Alarme");
setAlarmButton.setOnAction(e -> setAlarm());
HBox hbox = new HBox(timeLabel, dateLabel);
hbox.setSpacing(20);
hbox.setAlignment(Pos.CENTER);
HBox alarmBox = new HBox(hourLabel, hourField, minuteLabel, minuteField,dateLabel2, datePicker, setAlarmButton);
alarmBox.setSpacing(5);
hbox.setAlignment(Pos.CENTER);
VBox vbox = new VBox(hbox, alarmBox);
vbox.setSpacing(10);
Scene scene = new Scene(vbox, 580, 150);
primaryStage.setScene(scene);
primaryStage.show();
// Inicia o relógio em segundo plano
Task<Void> clockTask = createClockTask();
Thread clockThread = new Thread(clockTask);
clockThread.setDaemon(true);
clockThread.start();
}
private Task<Void> createClockTask() {
return new Task<Void>() {
@Override
protected Void call() {
TimerTask timerTask = new TimerTask() {
public void run() {
Platform.runLater(() -> {
datePicker.setValue(LocalDate.now());
LocalTime currentTime = LocalTime.now();
String timeString = currentTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
timeLabel.setText(timeString); dateLabel.setText(LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
LocalDate currentDate = LocalDate.now();
String dateString = currentDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
dateLabel.setText(dateString);
if (!alarmTriggered && alarmDateTime != null && LocalDateTime.now().isAfter(alarmDateTime)) {
showAlert("ACORDA VIADO!");
alarmTriggered = true;
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 0, 1000); // Executa a cada 1 segundo
return null;
}
};
}
private void setAlarm() {
String hourText = hourField.getText();
String minuteText = minuteField.getText();
LocalDate selectedDate = datePicker.getValue();
try {
int hour = Integer.parseInt(hourText);
int minute = Integer.parseInt(minuteText);
if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
showAlert("Hora inválida! Por favor, insira uma hora válida (0-23) e um minuto válido (0-59).");
} else if (selectedDate == null) {
showAlert("Data inválida! Por favor, selecione uma data.");
} else {
alarmDateTime = LocalDateTime.of(selectedDate, LocalTime.of(hour, minute));
showAlert("Alarme definido para " + alarmDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
}
} catch (NumberFormatException e) {
showAlert("Formato de hora ou minuto inválido! Por favor, insira números inteiros.");
}
}
private void showAlert(String message) {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Relógio");
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(null);
alert.showAndWait();
});
}
public static void main(String[] args) {
launch(args);
}
}