Java Chapter 15 Questions with Verified
Solutions
Analyze the following code.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Button btOK = new Button("OK");
Button btCancel = new Button("Cancel");
EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("The OK button is clicked");
}
};
btOK.setOnAction(handler);
btCancel.setOnAction(handler);
,HBox pane = new HBox(5);
pane.getChildren().addAll(btOK, btCancel);
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("Test"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in ✔️✔️A. When clicking the OK button, the
program displays The OK button is clicked.
B. When clicking the Cancel button, the program displays The OK button is clicked.
Analyze the following code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a button and place it in the scene
Button btOK = new Button("OK");
btOK.setOnAction(e -> System.out.println("OK 1"));
btOK.setOnAction(e -> System.out.println("OK 2"));
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
, primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. When clicking the button, the pr ✔️✔️C. When clicking the button, the program displays OK2.
Which of the following code correctly registers a handler with a button btOK?
A. btOK.setOnAction(e -> System.out.println("Handle the event"));
B. btOK.setOnAction((e) -> System.out.println("Handle the event"););
C. btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event"));
D. btOK.setOnAction(e -> {System.out.println("Handle the event");}); ✔️✔️A. btOK.setOnAction(e
-> System.out.println("Handle the event"));
B. btOK.setOnAction((e) -> System.out.println("Handle the event"););
C. btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event"));
D. btOK.setOnAction(e -> {System.out.println("Handle the event");});
Fill in the code below in the underline:
public class Test {
Solutions
Analyze the following code.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Button btOK = new Button("OK");
Button btCancel = new Button("Cancel");
EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("The OK button is clicked");
}
};
btOK.setOnAction(handler);
btCancel.setOnAction(handler);
,HBox pane = new HBox(5);
pane.getChildren().addAll(btOK, btCancel);
Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("Test"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in ✔️✔️A. When clicking the OK button, the
program displays The OK button is clicked.
B. When clicking the Cancel button, the program displays The OK button is clicked.
Analyze the following code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a button and place it in the scene
Button btOK = new Button("OK");
btOK.setOnAction(e -> System.out.println("OK 1"));
btOK.setOnAction(e -> System.out.println("OK 2"));
Scene scene = new Scene(btOK, 200, 250);
primaryStage.setTitle("MyJavaFX"); // Set the stage title
, primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited JavaFX
* support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
A. When clicking the button, the pr ✔️✔️C. When clicking the button, the program displays OK2.
Which of the following code correctly registers a handler with a button btOK?
A. btOK.setOnAction(e -> System.out.println("Handle the event"));
B. btOK.setOnAction((e) -> System.out.println("Handle the event"););
C. btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event"));
D. btOK.setOnAction(e -> {System.out.println("Handle the event");}); ✔️✔️A. btOK.setOnAction(e
-> System.out.println("Handle the event"));
B. btOK.setOnAction((e) -> System.out.println("Handle the event"););
C. btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event"));
D. btOK.setOnAction(e -> {System.out.println("Handle the event");});
Fill in the code below in the underline:
public class Test {