This commit is contained in:
lukas-heiligenbrunner 2020-04-10 18:26:03 +02:00
commit 2785236694
3 changed files with 168 additions and 0 deletions

122
src/sample/Controller.java Normal file
View File

@ -0,0 +1,122 @@
package sample;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.text.DecimalFormat;
import java.util.Date;
public class Controller {
public Button btnstart;
public Button btnpause;
public Button btnstop;
public Label timelabel;
public Button btnresume;
public TextField textinput;
public TextField entimefield;
private long starttime = new Date().getTime();
private long endtime;
private long warningtime;
private Thread curr = null;
private long pausetime = new Date().getTime();
private DecimalFormat format = new DecimalFormat("00");
private final Runnable timer = new Runnable() {
@Override
public void run() {
while (true) {
long currtime = new Date().getTime() - starttime;
int sec = (int) currtime / 1000;
int min = (int) currtime / (1000 * 60);
int ms = (int) currtime - (sec * 1000 + min * (1000 * 60));
Platform.runLater(() -> timelabel.setText(format.format(min) + ":" + format.format(sec) + ":" + format.format((int) ((float) ms / 10) + 0.5)));
if (warningtime < currtime) {
timelabel.setStyle("-fx-background-color: #FF0000");
}
if (endtime < currtime) {
btnstart.setDisable(false);
btnstop.setDisable(true);
curr.stop();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
public Controller() {
Platform.runLater(() -> {
btnstop.setDisable(true);
btnpause.setDisable(true);
btnresume.setDisable(true);
});
}
public void startclick(ActionEvent actionEvent) {
btnstart.setDisable(true);
btnpause.setDisable(false);
btnstop.setDisable(false);
starttime = new Date().getTime();
// parse warning field
String inp = textinput.getText();
int mins = Integer.parseInt(inp.substring(0, 2));
int secs = Integer.parseInt(inp.substring(3, 5));
int mss = Integer.parseInt(inp.substring(6, 8));
warningtime = mss + secs * 1000 + mins * 60 * 1000;
// parse end field
String inpe = entimefield.getText();
int mine = Integer.parseInt(inpe.substring(0, 2));
int sece = Integer.parseInt(inpe.substring(3, 5));
int mse = Integer.parseInt(inpe.substring(6, 8));
endtime = mse + sece * 1000 + mine * 60 * 1000;
timelabel.setStyle("-fx-background-color: #ebebeb");
curr = new Thread(timer);
curr.start();
}
public void pauseclick(ActionEvent actionEvent) {
btnpause.setDisable(true);
btnresume.setDisable(false);
pausetime = new Date().getTime();
curr.stop();
}
public void stopclick(ActionEvent actionEvent) {
btnstart.setDisable(false);
btnstop.setDisable(true);
btnpause.setDisable(true);
btnresume.setDisable(true);
curr.stop();
}
public void resumeclick(ActionEvent actionEvent) {
btnresume.setDisable(true);
btnpause.setDisable(false);
starttime += (new Date().getTime() - pausetime);
curr = new Thread(timer);
curr.start();
}
}

24
src/sample/Main.java Normal file
View File

@ -0,0 +1,24 @@
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Stoppuhr");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 300, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

22
src/sample/sample.fxml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="313.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="btnstart" layoutX="31.0" layoutY="34.0" mnemonicParsing="false" onAction="#startclick" text="Start" />
<Button fx:id="btnpause" layoutX="90.0" layoutY="34.0" mnemonicParsing="false" onAction="#pauseclick" text="Pause" />
<Button fx:id="btnstop" layoutX="240.0" layoutY="34.0" mnemonicParsing="false" onAction="#stopclick" text="Stop" />
<Label fx:id="timelabel" layoutX="31.0" layoutY="95.0" text="00:00:00">
<font>
<Font size="52.0" />
</font></Label>
<Button fx:id="btnresume" layoutX="157.0" layoutY="34.0" mnemonicParsing="false" onAction="#resumeclick" text="Resume" />
<TextField fx:id="textinput" layoutX="62.0" layoutY="200.0" text="02:50:89" />
<TextField fx:id="entimefield" layoutX="63.0" layoutY="280.0" text="05:00:00" />
<Label layoutX="63.0" layoutY="176.0" text="Warningtime" />
<Label layoutX="63.0" layoutY="257.0" text="Endtime" />
</children>
</AnchorPane>