The buttons increment or decrement a counter.
There are two labels, first one is bound using if-then-else (using When) to see if number is 0 or greater. If so, it prints "positive". The number 0 is positive since the sign bit is 0 indicating a positive number.
The second label uses a .concat operation to join two Strings.
package ex37;
import javafx.application.Application;
import javafx.beans.binding.When;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Ex37 extends Application {
@Override
public void start(Stage primaryStage) {
IntegerProperty labelNumber
= new SimpleIntegerProperty(0);
Button inc = new Button("+");
inc.setOnAction( e -> {
labelNumber.set(labelNumber.get() + 1);
});
Button dec = new Button("-");
dec.setOnAction( e -> {
labelNumber.set(labelNumber.get() - 1);
});
Label label1 = new Label();
Label label2 = new Label();
VBox root = new VBox(inc, dec, label1, label2);
root.setSpacing(50);
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-font-size: 16pt;"
+ "-fx-background-color: aliceblue;");
label1.textProperty()
.bind(new When(labelNumber
.greaterThanOrEqualTo(0))
.then("positive")
.otherwise("negative"));
label2.textProperty()
.bind(new SimpleStringProperty("Number: ")
.concat(labelNumber.asString()));
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Example 37. Fluent API 1");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is the output:
No comments:
Post a Comment