Please consider the following JavaFX application. You will be asked questions regarding…
- User
Please consider the following JavaFX application. You will be asked questions regarding the output it produces and its behavior.
Source Code package javafxapplication40;
//For brevity, all import statements are omitted
public class JavaFXApplication40 extends Application {
@Override // Override the start method in the Application class public void start(Stage primaryStage) { GridPane pane = new GridPane(); // Create a pane and set its properties pane.setAlignment(Pos.CENTER); // place all around center of the container pane.setPadding(new Insets(15, 15, 15, 15)); // top, right,bottom, left padding pane.setHgap(6); // internal padding pane.setVgap(6);
code// Place nodes in the pane pane.add(new Label("Name:"), 0, 0); TextField tfName = new TextField(); pane.add(tfName, 1, 0); Label lblMessage = new Label(); pane.add(lblMessage, 1, 1); Button btAdd = new Button("Click me!"); pane.add(btAdd, 1, 3); GridPane.setHalignment(btAdd, HPos.RIGHT); // Event handler for button click btAdd.setOnAction(e -> { String name = tfName.getText(); // Get the text from the text field lblMessage.setText("Hello " + name); // Set the label with the concatenated message btAdd.setVisible(false); }); Button btExit = new Button("Exit"); // Button to terminate the app pane.add(btExit, 1, 4); GridPane.setHalignment(btExit, HPos.RIGHT); // Event handler for 'Exit' button btExit.setOnAction(e -> { Platform.exit(); // Terminate the application }); // Create a scene and place it in the stage Scene scene = new Scene(pane, 400, 250); // Set width and height of the scene primaryStage.setTitle("ShowGridPane"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } public static void main(String[] args) { launch(args); } - Assistant
1. Text in
lblMessageAfter Clicking "Click me!"When the "Click me!" button is pressed, the event handler retrieves the text entered in the
tfNametext field and updates thelblMessagelabel. Specifically, the label's text is set to:code"Hello " + nameExplanation:
- If the user enters "Alice" in the text field,
lblMessagewill display:codeHello Alice - If the text field is empty,
lblMessagewill simply show:codeHello
2. Behavior of the “Click me!” Button After Click
After the "Click me!" button is clicked, the following line of code is executed:
javabtAdd.setVisible(false);Effect:
- The button becomes in the user interface.
- If the user enters "Alice" in the text field,