Chapter 15 Event-Driven Programming and Animations

Section 15.2 Events and Event Sources
1.    A JavaFX action event handler is an instance of _______.
a.    ActionEvent
b.    Action
c.    EventHandler
d.    EventHandler<ActionEvent>
Key:d
action event handler动作时间处理器必须是接口 EventHandler<ActionEvent>的一个实例,并且还需要覆盖handle(ActionEvent)方法,用于处理动作时间

#
2.    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)
Key:c

#
3.    A JavaFX event handler for event type T is an instance of _______.
a.    ActionEvent
b.    Action
c.    EventHandler
d.    EventHandler<T>
Key:d

#
4.    Which of the following are the classes in JavaFX for representing an event?
a.    ActionEvent
b.    MouseEvent
c.    KeyEvent
d.    WindowEvent
Key:abcd

#
5.    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)
Key:b

#
6.    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.
Key:bd
事件是从一个事件源上产生的对象,事件源触发事件。

触发一个事件意味着产生一个事件并委派处理器处理该事件。

#
7.    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.
Key:abcd

#
8.    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.
Key:abcd

#
9.    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.
Key:bcde

#
Section 15.4 Inner Classes
10. 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.
Key:abcd
内部类可以使程序变得简单和简洁

内部类可以使用可见性修饰符所定义,和应用于一个类成员的可见性原则相同

内部类可以被定义为static,一个static的内部类可以使用外部类的名字所访问

#
11. 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
Key:b

#
12.     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.
Key:e
可以访问封闭对象中的私有实例变量
#
Section 15.5 Anonymous Class Handlers
13. 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.
Key:abcde
一个匿名内部类是一个没有名字的内部类

一个匿名内部类总是从一个父类继承或者实现一个接口,但它不能有显式的extends或者implements子句

一个匿名内部类必选实现父类或者接口中的所有抽象方法

一个匿名内部类总是使用它父类的无参构造方法来创建一个实例

一个匿名内部类被编译成一个名为 OuterClassName$n.class的类
#
14. 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
Key:d

#
15. 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<>() {
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.
Key:c

#
16. 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.
Key:ab

#
Section 15.6 Simplifying Event Handing Using Lambda Expressions
17. 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.
Key:c

#
18. 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");});
Key:abcd

#
19. 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! ")}
Key:a

#
20. 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);}
Key:bcd

#
21. 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;}
Key:bc
如果只有一个参数,并且没有显示的数据类型,圆括号可以被省略
#
22. 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.
Key:bc
接口如果包含多个方法,编译器将无法编译lambda表达式,接口必须只包含一个抽象的方法,这样的接口称为功能接口(functional interface)
#
Section 15.8 Mouse Events
23. 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);
Key:a

#
24. 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()));
Key:c

#
Section 15.9 Key Events
25. 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);
Key:d

#
26. 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());}
Key:bcd

#
27. 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
Key:a

#
28. 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
Key:d

#
Section 15.10 Listeners for Observable Objects
29. 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.
Key:b

#
Section 15.11 Animation
30. Which of the following methods is not defined in the Animation class?
a. pause()
b. play()
c. stop()
d. resume()
Key:d
pause()  暂停动画
play()     从当前位置播放动画
stop()     停止动画并重置动画
#
31. The properties _________ are defined in the Animation class.
a. autoReverse
b. cycleCount
c. rate
d. status
Key:abcd

#
32. The properties _________ are defined in the PathTransition class.
a. duration
b. node
c. orientation
d. path
Key:abcd

#
33. To properties _________ are defined in the FadeTransition class.
a. duration
b. node
c. fromValue
d. toValue
e. byValue
Key:abcde

#
34. 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)
Key:cd

#
35. __________ is a subclass of Animation.
a. PathTransition
b. FadeTransition
c. Timeline
d. Duration
Key:abc
PathTransition、FadeTransition、Timeline都是Animation的子类

Java题库——Chapter15 事件驱动编程和动画的更多相关文章

  1. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  2. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  3. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  4. Java题库——Chapter11 继承和多态

    1)Analyze the following code: public class Test { public static void main(String[ ] args) { B b = ne ...

  5. JAVA题库01

    说出一些常用的类,包,接口,请各举5个 常用的类:BufferedReader  BufferedWriter  FileReader  FileWirter  String Integer java ...

  6. Java题库——Chapter17 二进制I/0

    Introduction to Java Programming 的最后一章,完结撒花!Chapter 17 Binary I/O Section 17.2 How is I/O Handled in ...

  7. Java题库——Chapter16 JavaFX UI组件和多媒体

    Chapter 16 JavaFX UI Controls and Multimedia Section 16.2 Labeled and Label1. To create a label with ...

  8. Java题库——Chapter14 JavaFX基础

    Chapter 14 JavaFX Basics Section 14.2 JavaFX vs Swing and AWT1. Why is JavaFX preferred?a. JavaFX is ...

  9. Java题库——Chapter10 面向对象思考

    1)You can declare two variables with the same name in ________. 1) _______ A)a method one as a forma ...

随机推荐

  1. 请问1^x+2^x+3^x+\cdots +n^x的算式是什么呢?

    目录 总结 请问\(1^x+2^x+3^x+\cdots +n^x\)的算式是什么呢? 一.求和式\(\sum\limits_{i=1}^n{i}\)的算式 如何证明求和简式\(\sum_{i=1}^ ...

  2. SpringBoot系列之集成jsp模板引擎

    目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...

  3. MySQL必知必会(通配符过滤Like,%,_)

    SELECT prod_id, prod_name FROM products WHERE prod_name LIKE 'jet%'; #百分号(%)表示任何字符出现任意次数, %不能匹配值为NUL ...

  4. luogu P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team

    题目背景 老唐最近迷上了飞盘,约翰想和他一起玩,于是打算从他家的N头奶牛中选出一支队伍. 每只奶牛的能力为整数,第i头奶牛的能力为R i .飞盘队的队员数量不能少于 1.大于N.一 支队伍的总能力就是 ...

  5. hdu2199,double二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 题意:给一个Y,求满足8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y的 ...

  6. SQL Server导入mdf数据库文件

    方法一: 1.新建查询然后输入如下代码,点击F5键或者点击运行按钮即可 EXEC sp_attach_db @dbname = '你的数据库名', @filename1 = 'mdf文件路径(包缀名) ...

  7. Java 从入门到进阶之路(十三)

    在之前的文章我们介绍了一下 Java 类的 private,static,final,本章我们来看一下 Java 中的抽象类和抽象方法. 我们先来看下面一段代码: // 根据周长求面积 class S ...

  8. 开局一张图,学一学项目管理神器Maven!

    Maven强大的Java工程构建工具,做Java开发时少了跟Maven打交道,之前在知乎上看到有人提问:"学Java开发需不需要学习Maven?",个人认为是必需要学的,这和工欲善 ...

  9. Web基础了解版02-JavaScript

    JavaScript 特性 ① 解释型语言.JavaScript是一种解释型的脚本语言,JavaScript是在程序的运行过程中逐行进行解释,不需要被编译为机器码再执行. ② 面向对象.JavaScr ...

  10. 【搞定Jvm面试】 面试官:谈谈 JVM 类加载过程是怎样的?

    类加载过程 Class 文件需要加载到虚拟机中之后才能运行和使用,那么虚拟机是如何加载这些 Class 文件呢? 系统加载 Class 类型的文件主要三步:加载->连接->初始化.连接过程 ...