Java Chapter 15
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(); } A. () -> System.out.print("Action 1! ") B. (e) -> System.out.print("Action 1! ") C. System.out.print("Action 1! ") D. (e) -> {System.out.print("Action 1! ")}
A. () -> System.out.print("Action 1! ")
Which of the following statements are true? A. A Button can fire an ActionEvent. B. A Button can fire a MouseEvent. C. A Button can fire a KeyEvent. D. A TextField can fire an ActionEvent.
A. A Button can fire an ActionEvent. B. A Button can fire a MouseEvent. C. A Button can fire a KeyEvent. D. A TextField can fire an ActionEvent.
Which of the following statements are true? A. A Node can fire an ActionEvent. B. A Noden can fire a MouseEvent. C. A Node can fire a KeyEvent. D. A Scene can fire a MouseEvent.
A. A Node can fire an ActionEvent. B. A Noden can fire a MouseEvent. C. A Node can fire a KeyEvent. D. A Scene can fire a MouseEvent.
Which of the following are the classes in JavaFX for representing an event? A. ActionEvent B. MouseEvent C. KeyEvent D. WindowEvent
A. ActionEvent B. MouseEvent C. KeyEvent D. WindowEvent
Which of the following statements are true? A. An anonymous inner class is an inner class without a name. B. An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. C. An anonymous inner class must implement all the abstract methods in the superclass or in the interface. D. An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object(). E. An anonymous inner class is compiled into a class named OuterClassName$n.class.
A. An anonymous inner class is an inner class without a name. B. An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. C. An anonymous inner class must implement all the abstract methods in the superclass or in the interface. D. An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object(). E. An anonymous inner class is compiled into a class named OuterClassName$n.class.
Which of the following statements are true? A. Inner classes can make programs simple and concise. B. An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. C. An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class. D. An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
A. Inner classes can make programs simple and concise. B. An inner class can be declared public or private subject to the same visibility rules applied to a member of the class. C. An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class. D. An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class.
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 { @Override 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); } } A. Key pressed B Key typed UNDEFINED B. Key pressed B Key typed C. Key typed UNDEFINED D. Key pressed B
A. Key pressed B Key typed UNDEFINED
__________ is a subclass of Animation. A. PathTransition B. FadeTransition C. Timeline D. Duration
A. PathTransition B. FadeTransition C. Timeline
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 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 OK button, the program displays The OK button is clicked. B. When clicking the Cancel button, the program displays The OK button is clicked. C. When clicking either button, the program displays The OK button is clicked twice. D. The program has a runtime error, because the handler is registered with more than one source.
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.
The properties _________ are defined in the Animation class. A. autoReverse B. cycleCount C. rate D. status
A. autoReverse B. cycleCount C. rate D. status
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");});
To properties _________ are defined in the FadeTransition class. A. duration B. node C. fromValue D. toValue E. byValue
A. duration B. node C. fromValue D. toValue E. byValue
The properties _________ are defined in the PathTransition class. A. duration B. node C. orientation D. path
A. duration B. node C. orientation D. path
To handle the mouse click event on a pane p, register the handler with p using ______. A. p.setOnMouseClicked(handler); B. p.setOnMouseDragged(handler); C. p.setOnMouseReleased(handler); D. p.setOnMousePressed(handler);
A. p.setOnMouseClicked(handler);
Fill in the code below in the underline: public class Test { public static void main(String[] args) { Test test = new Test(); test.setAction2(______________________________); } public void setAction2(T2 t) { t.m(4.5); } } interface T2 { public void m(Double d); } A. () -> System.out.print(e) B. (e) -> System.out.print(e) C. e -> System.out.print(e) D. (e) -> {System.out.print(e);}
B. (e) -> System.out.print(e) C. e -> System.out.print(e) D. (e) -> {System.out.print(e);}
Fill in the code below in the underline: public class Test { public static void main(String[] args) { Test test = new Test(); System.out.println(test.setAction3(_____________)); } public double setAction3(T3 t) { return t.m(5.5); } } interface T3 { public double m(Double d); } A. () -> e * 2 B. (e) -> e * 2 C. e -> e * 2 D. (e) -> {e * 2;}
B. (e) -> e * 2 C. e -> e * 2
Fill in the code to display the key pressed in the text. import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.text.Text; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Pane pane = new Pane(); Text text = new Text(20, 20, "Welcome"); pane.getChildren().add(text); 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 text.setFocusTraversable(true); text.setOnKeyPressed(_______________________); } /** * 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. () -> text.setText(e.getText()) B. (e) -> text.setText(e.getText()) C. e -> text.setText(e.getText()) D. e -> {text.setText(e.getText());}
B. (e) -> text.setText(e.getText()) C. e -> text.setText(e.getText()) D. e -> {text.setText(e.getText());}
Which of the following statements are true? A. A Shape can fire an ActionEvent. B. A Shape can fire a MouseEvent. C. A Shape can fire a KeyEvent. D. A Text is a Shape. E. A Circle is a Shape.
B. A Shape can fire a MouseEvent. C. A Shape can fire a KeyEvent. D. A Text is a Shape. E. A Circle is a Shape.
Which of the following statements are true? A. A handler object fires an event. B. A source object fires an event. C. Any object such a String object can fire an event. D. A handler is registered with the source object for processing the event.
B. A source object fires an event. D. A handler is registered with the source object for processing the event.
Suppose A is an inner class in Test. A is compiled into a file named _________. A. A.class B. Test$A.class C. A$Test.class D. Test&A.class
B. Test$A.class
Analyze the following code: import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; public class Test { public static void main(String[] args) { DoubleProperty balance = new SimpleDoubleProperty(); balance.addListener(ov -> System.out.println(2 + balance.doubleValue())); balance.set(4.5); } } A. The program displays 4.5. B. The program displays 6.5. C. The program would display 4.5 if the balance.set(4.5) is placed before the balance.addLisnter(...) statement. D. The program would display 6.5 if the balance.set(4.5) is placed before the balance.addLisnter(...) statement.
B. The program displays 6.5.
Analyze the following code: public class Test { public static void main(String[] args) { Test test = new Test(); test.setAction(() -> System.out.print("Action 1! ")); } public void setAction(T t) { t.m1(); } } interface T { public void m1(); public void m2(); } A. The program displays Action 1. B. The program has a compile error because T is not a functional interface. T contains multiple methods. C. The program would work if you delete the method m2 from the interface T. D. The program has a runtime error because T is not a functional interface. T contains multiple methods.
B. The program has a compile error because T is not a functional interface. T contains multiple methods. C. The program would work if you delete the method m2 from the interface T.
To register a source for an action event with a handler, use __________. A. source.addAction(handler) B. source.setOnAction(handler) C. source.addOnAction(handler) D. source.setActionHandler(handler)
B. source.setOnAction(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 // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); btOK.setOnAction(new EventHandler<ActionEvent>() { 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); } } A. The program has a compile error because no handlers are registered with btOK. B. The program has a runtime error because no handlers are registered with btOK. C. The message "The OK button is clicked" is displayed when you click the OK button. D. The handle method is not executed when you click the OK button, because no handler is registered with btOK.
C. The message "The OK button is clicked" is displayed when you click the OK button.
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 program displays OK1 OK2. B. When clicking the button, the program displays OK1. C. When clicking the button, the program displays OK2. D. The program has a compile error, because the setOnAction method is invoked twice.
C. When clicking the button, the program displays OK2.
To create a KeyFrame with duration 1 second, use ______________. A. new KeyFrame(1000, handler) B. new KeyFrame(1, handler) C. new KeyFrame(Duration.millis(1000), handler) D. new KeyFrame(Duration.seconds(1), handler)
C. new KeyFrame(Duration.millis(1000), handler) D. new KeyFrame(Duration.seconds(1), handler)
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 // 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); } } A. pane.setOnMouseClicked((e) -> System.out.println(e.getX() + ", " + e.getY())); B. pane.setOnMouseReleased(e -> {System.out.println(e.getX() + ", " + e.getY())}); C. pane.setOnMousePressed(e -> System.out.println(e.getX() + ", " + e.getY())); D. pane.setOnMouseDragged((e) -> System.out.println(e.getX() + ", " + e.getY()));
C. pane.setOnMousePressed(e -> System.out.println(e.getX() + ", " + e.getY()));
A JavaFX action event handler contains a method ________. A. public void actionPerformed(ActionEvent e) B. public void actionPerformed(Event e) C. public void handle(ActionEvent e) D. public void handle(Event e)
C. public void handle(ActionEvent e)
A JavaFX action event handler is an instance of _______. A. ActionEvent B. Action C. EventHandler D. EventHandler<ActionEvent>
D. EventHandler<ActionEvent>
A JavaFX event handler for event type T is an instance of _______. A. ActionEvent B. Action C. EventHandler D. EventHandler<T>
D. EventHandler<T>
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 { @Override 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); } } A. Key pressed DOWN Key typed UNDEFINED B. Key pressed DOWN Key typed C. Key typed UNDEFINED D. Key pressed DOWN
D. Key pressed DOWN
Suppose A is an anonymous inner class in Test. A is compiled into a file named _________. A. A.class B. Test$A.class C. A$Test.class D. Test$1.class E. Test&1.class
D. Test$1.class
To handle the key pressed event on a pane p, register the handler with p using ______. A. p.setOnKeyClicked(handler); B. p.setOnKeyTyped(handler); C. p.setOnKeyReleased(handler); D. p.setOnKeyPressed(handler);
D. p.setOnKeyPressed(handler);
Which of the following methods is not defined in the Animation class? A. pause() B. play() C. stop() D. resume()
D. resume()
Which statement is true about a non-static inner class? A. It must implement an interface. B. It is accessible from any other class. C. It can only be instantiated in the enclosing class. D. It must be final if it is declared in a method scope. E. It can access private instance variables in the enclosing object.
E. It can access private instance variables in the enclosing object.