Chapter 15 Flashcards

1
Q

To handle the key pressed event on a pane p, register the handler with p using ______.

A

p.setOnKeyPressed(handler);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To handle the mouse click event on a pane p, register the handler with p using ______.

A

p.setOnMouseClicked(handler);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
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);
 } }
A

The message “The OK button is clicked” is displayed when you click the OK button.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Suppose A is an inner class in Test. A is compiled into a file named _________.

A

Test$A.class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
[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();
}
A

(e) -> {System.out.print(“Action 1! “)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A JavaFX event handler for event type T is an instance of _______.

A

EventHandler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
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);
 } }
A

pane.setOnMousePressed(e -> System.out.println(e.getX() + “, “ + e.getY()));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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);
}
}

A

When clicking the button, the program displays OK2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following methods is not defined in the Animation class?

A

resume()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Suppose A is an anonymous inner class in Test. A is compiled into a file named _________.

A

Test$1.class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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);
}
}

A

Key pressed DOWN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A JavaFX action event handler is an instance of _______.

A

EventHandler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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);
 } }
A

Key pressed B Key typed UNDEFINED

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A JavaFX action event handler contains a method ________.

A

public void handle(ActionEvent e)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

It can access private instance variables in the enclosing object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

To register a source for an action event with a handler, use __________.

A

source.setOnAction(handler)