In: Computer Science
URGENT!!
DO THIS CODE IN JAVA
Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class buttonExample extends Application {
public void start(Stage stage)
{
//for title
stage.setTitle("Button
Example");
//for button
Button button = new
Button("Button");
//for pane
TilePane pane = new
TilePane();
//for label
final Label label = new
Label("0");
//for incrementing value
final int[] arr = new int[1];
arr[0] = 0;
//for action when clicking on
button
EventHandler<ActionEvent>
event = new EventHandler<ActionEvent>() {
public void
handle(ActionEvent e)
{
label.setText(label.getText()+","+(++arr[0]));
}
};
//setting action
button.setOnAction(event);
//adding components
pane.getChildren().add(button);
pane.getChildren().add(label);
//setting frame size
Scene sc = new Scene(pane, 500,
500);
stage.setScene(sc);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}