47 lines
1.7 KiB
Java
47 lines
1.7 KiB
Java
import javafx.application.Application;
|
|
import javafx.collections.ObservableList;
|
|
import javafx.event.ActionEvent;
|
|
import javafx.event.EventHandler;
|
|
import javafx.geometry.Insets;
|
|
import javafx.geometry.Pos;
|
|
import javafx.scene.Node;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.CheckBox;
|
|
import javafx.scene.control.Label;
|
|
import javafx.scene.layout.BorderPane;
|
|
import javafx.scene.layout.StackPane;
|
|
import javafx.scene.layout.VBox;
|
|
import javafx.stage.Stage;
|
|
import java.util.Random;
|
|
|
|
public class RandomWalk extends Application {
|
|
private Button button = new Button();
|
|
private int counter = 0;
|
|
private Label label = new Label("" + this.counter);
|
|
private final Random random = new Random();
|
|
@Override
|
|
public void start(Stage primaryStage) throws Exception {
|
|
primaryStage.setTitle("Random Walk");
|
|
this.button.setText((random.nextBoolean() ? "Up" : "Down"));
|
|
BorderPane borderPane = new BorderPane();
|
|
borderPane.setPadding(new Insets(20, 20,20,20));
|
|
borderPane.setCenter(this.button);
|
|
BorderPane.setAlignment(this.button, Pos.CENTER);
|
|
borderPane.setBottom(label);
|
|
BorderPane.setAlignment(label, Pos.BOTTOM_CENTER);
|
|
Scene scene = new Scene(borderPane, 300, 250);
|
|
primaryStage.setScene(scene);
|
|
this.button.setOnAction(e -> {
|
|
counter += (button.getText().equals("Up") ? 1 : -1);
|
|
button.setText((random.nextBoolean() ? "Up" : "Down"));
|
|
label.setText("" + counter);
|
|
});
|
|
primaryStage.show();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
launch(args);
|
|
}
|
|
}
|