Chapter 15 Flashcards
To handle the key pressed event on a pane p, register the handler with p using ______.
p.setOnKeyPressed(handler);
To handle the mouse click event on a pane p, register the handler with p using ______.
p.setOnMouseClicked(handler);
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.stage.Stage; public class Test extends Application { // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK");
btOK.setOnAction(new EventHandler() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } });
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); } }
The message “The OK button is clicked” is displayed when you click the OK button.
Suppose A is an inner class in Test. A is compiled into a file named _________.
Test$A.class
[X]Fill in the code below in the underline: public class Test { public static void main(String[] args) { Test test = new Test(); test.setAction(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); } public void setAction(T1 t) { t.m(); } } interface T1 { public void m(); }
(e) -> {System.out.print(“Action 1! “)}
A JavaFX event handler for event type T is an instance of _______.
EventHandler
Fill in the code in the underlined location to display the mouse point location when the mouse is pressed in the pane. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; public class Test extends Application { // Override the start method in the Application class public void start(Stage primaryStage) { Pane pane = new Pane(); \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Scene scene = new Scene(pane, 200, 250); primaryStage.setTitle("Test"); // 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); } }
pane.setOnMousePressed(e -> System.out.println(e.getX() + “, “ + e.getY()));
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 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);
}
}
When clicking the button, the program displays OK2.
Which of the following methods is not defined in the Animation class?
resume()
Suppose A is an anonymous inner class in Test. A is compiled into a file named _________.
Test$1.class
Supose the follwoing program displays a pane in the stage. What is the output if the user presses the DOWN arrow key?
import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; // import javafx classes omitted public class Test1 extends Application {
public void start(Stage primaryStage) { // Code to create and display pane omitted Pane pane = new Pane(); Scene scene = new Scene(pane, 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage pane.requestFocus(); pane.setOnKeyPressed(e -> System.out.print("Key pressed " + e.getCode() + " ")); pane.setOnKeyTyped(e -> System.out.println("Key typed " + e.getCode())); }
/**
* 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);
}
}
Key pressed DOWN
A JavaFX action event handler is an instance of _______.
EventHandler
Suppose the following program displays a pane in the stage. What is the output if the user presses the key for letter B? import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; // import javafx classes omitted public class Test1 extends Application {
public void start(Stage primaryStage) { // Code to create and display pane omitted Pane pane = new Pane(); Scene scene = new Scene(pane, 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage pane.requestFocus(); pane.setOnKeyPressed(e -> System.out.print("Key pressed " + e.getCode() + " ")); pane.setOnKeyTyped(e -> System.out.println("Key typed " + e.getCode())); } /** * 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); } }
Key pressed B Key typed UNDEFINED
A JavaFX action event handler contains a method ________.
public void handle(ActionEvent e)
Which statement is true about a non-static inner class?
It must implement an interface.
It is accessible from any other class.
It can only be instantiated in the enclosing class.
It must be final if it is declared in a method scope.
It can access private instance variables in the enclosing object.
It can access private instance variables in the enclosing object.