diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-27 21:32:05 +0200 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-04-27 21:32:05 +0200 |
| commit | 56fec9fd33949003ffdcb1132a2aa2a6a3cf444f (patch) | |
| tree | 5fa5a19ead33b4bc2a10004e7870a488711c656b /src | |
| parent | 31b7641ec7724f7eb2b5833c71705d176cb1bfdf (diff) | |
| download | Java2-56fec9fd33949003ffdcb1132a2aa2a6a3cf444f.tar.gz Java2-56fec9fd33949003ffdcb1132a2aa2a6a3cf444f.zip | |
Add a button that fires automatically after a given interval.
Diffstat (limited to 'src')
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java | 29 | ||||
| -rw-r--r-- | src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java | 97 |
2 files changed, 126 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java new file mode 100644 index 0000000..5af329e --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownButtonMain.java @@ -0,0 +1,29 @@ +package de.fhswf.in.inf.java2.aufgabe04; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class CountdownButtonMain extends Application +{ + + @Override + public void start(Stage primaryStage) + { + CountdownExitButton ceb = new CountdownExitButton("Ok", 30); + ceb.setOnAction(e -> { + Platform.exit(); + }); + Scene scene = new Scene(ceb); + primaryStage.setScene(scene); + primaryStage.show(); + ceb.startCountdown(); + + } + + public static void main(String[] args) + { + launch(args); + } +} diff --git a/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java new file mode 100644 index 0000000..b859b8f --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe04/CountdownExitButton.java @@ -0,0 +1,97 @@ +/** + * File for the CountdownExitButton class. + */ + +package de.fhswf.in.inf.java2.aufgabe04; + +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.scene.control.Button; +import javafx.util.Duration; + +/** + * A class that fires automatically after a given interval. + * + * @author Stefan Suhren + * @version 1.0 + */ +public class CountdownExitButton extends Button +{ + + private int countdown = 30; + + private Timeline timeline = new Timeline(); + + /** + * A Button that exits the application. + * + * @param text + * The text that will be displayed + * @param countdown + * The time in seconds till the button fires + */ + public CountdownExitButton(String text, int countdown) + { + super(text); + setCountdown(countdown); + + timeline.setCycleCount(Timeline.INDEFINITE); + timeline + .getKeyFrames() + .add(new KeyFrame( + Duration.seconds(1), + (e -> { + CountdownExitButton.this + .setText(CountdownExitButton.this + .getText() + .replaceFirst( + "\\(" + + CountdownExitButton.this.countdown + + "\\)$", + "(" + + --CountdownExitButton.this.countdown + + ")")); + if (CountdownExitButton.this.countdown <= 0) + { + CountdownExitButton.this.fire(); + } + }))); + } + + /** + * Starts the countdown. + * + */ + public void startCountdown() + { + super.setText(getText() + " (" + countdown + ")"); + timeline.playFromStart(); + } + + /** + * Set the countdown. + * + * @param countdown + * The time in seconds till the button fires. + */ + public void setCountdown(int countdown) + { + if (countdown < 0) + { + throw new IllegalArgumentException("Countdown can't be negative."); + } + this.countdown = countdown; + } + + /** + * Get the countdown. + * + * @return + * The countdown in seconds. + */ + public int getCountdown() + { + return countdown; + } + +} |
