1
Final Test Review: Programming Exercises
Exercise 1.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ControlCircle extends Application {
private CirclePane circlePane = new CirclePane();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
[Link](10);
[Link]([Link]);
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
[Link]().add(btEnlarge);
[Link]().add(btShrink);
1
2
// Create and register the handler
[Link](new EnlargeHandler());
BorderPane borderPane = new BorderPane();
[Link](circlePane);
[Link](hBox);
[Link](hBox, [Link]);
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 200, 150);
[Link]("ControlCircle"); // Set the stage title
[Link](scene); // Place the scene in the stage
[Link](); // Display the stage
}
class EnlargeHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
[Link]();
}
}
/**
* 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) {
2
3
launch(args);
}
}
class CirclePane extends StackPane {
private Circle circle = new Circle(50);
public CirclePane() {
getChildren().add(circle);
[Link]([Link]);
[Link]([Link]);
}
public void enlarge() {
[Link]([Link]() + 2);
}
public void shrink() {
[Link]([Link]() > 2 ?
[Link]() - 2 : [Link]());
}
}
Exercise 2.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
3
4
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ButtonDemo extends Application {
protected Text text = new Text(50, 50, "JavaFX Programming");
protected BorderPane getPane() {
HBox paneForButtons = new HBox(20);
Button btLeft = new Button("Left",
new ImageView("image/[Link]"));
Button btRight = new Button("Right",
new ImageView("image/[Link]"));
[Link]().addAll(btLeft, btRight);
[Link]([Link]);
[Link]("-fx-border-color: green");
BorderPane pane = new BorderPane();
[Link](paneForButtons);
Pane paneForText = new Pane();
[Link]().add(text);
[Link](paneForText);
[Link](e -> [Link]([Link]() - 10));
[Link](e -> [Link]([Link]() + 10));
4
5
return pane;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a scene and place it in the stage
Scene scene = new Scene(getPane(), 450, 200);
[Link]("ButtonDemo"); // Set the stage title
[Link](scene); // Place the scene in the stage
[Link](); // 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);
}
}
Exercise 3.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
5
6
import [Link];
import [Link];
import [Link];
public class CheckBoxDemo extends ButtonDemo {
@Override // Override the getPane() method in the super class
protected BorderPane getPane() {
BorderPane pane = [Link]();
Font fontBoldItalic = [Link]("Times New Roman",
[Link], [Link], 20);
Font fontBold = [Link]("Times New Roman",
[Link], [Link], 20);
Font fontItalic = [Link]("Times New Roman",
[Link], [Link], 20);
Font fontNormal = [Link]("Times New Roman",
[Link], [Link], 20);
[Link](fontNormal);
VBox paneForCheckBoxes = new VBox(20);
[Link](new Insets(5, 5, 5, 5));
[Link]("-fx-border-color: green");
CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic");
[Link]().addAll(chkBold, chkItalic);
[Link](paneForCheckBoxes);
6
7
EventHandler<ActionEvent> handler = e -> {
if ([Link]() && [Link]()) {
[Link](fontBoldItalic); // Both check boxes checked
}
else if ([Link]()) {
[Link](fontBold); // The Bold check box checked
}
else if ([Link]()) {
[Link](fontItalic); // The Italic check box checked
}
else {
[Link](fontNormal); // Both check boxes unchecked
}
};
[Link](handler);
[Link](handler);
return pane; // Return a new pane
}
/**
* 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);
}
7
8
Exercise 4.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class HandleEvent extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
HBox pane = new HBox(10);
[Link]([Link]);
Button btOK = new Button("OK");
Button btCancel = new Button("Cancel");
OKHandlerClass handler1 = new OKHandlerClass();
[Link](handler1);
CancelHandlerClass handler2 = new CancelHandlerClass();
[Link](handler2);
[Link]().addAll(btOK, btCancel);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
[Link]("HandleEvent"); // Set the stage title
8
9
[Link](scene); // Place the scene in the stage
[Link](); // 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);
}
}
class OKHandlerClass implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
[Link]("OK button clicked");
}
}
class CancelHandlerClass implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
[Link]("Cancel button clicked");
}
}
9
10
Exercise 5.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class KeyEventDemo extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
Pane pane = new Pane();
Text text = new Text(20, 20, "A");
[Link]().add(text);
[Link](e -> {
switch ([Link]()) {
case DOWN: [Link]([Link]() + 10); break;
case UP: [Link]([Link]() - 10); break;
case LEFT: [Link]([Link]() - 10); break;
case RIGHT: [Link]([Link]() + 10); break;
default:
if ([Link]().length() > 0)
[Link]([Link]());
}
});
// Create a scene and place the pane in the stage
10
11
Scene scene = new Scene(pane);
[Link]("KeyEventDemo"); // Set the stage title
[Link](scene); // Place the scene in the stage
[Link](); // Display the stage
[Link](); // text is focused to receive key input
}
/**
* 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);
}
}
Exercise 6.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MouseEventDemo extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
Pane pane = new Pane();
11
12
Text text = new Text(20, 20, "Programming is fun");
[Link]().addAll(text);
[Link](e -> {
[Link]([Link]());
[Link]([Link]());
});
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 300, 100);
[Link]("MouseEventDemo"); // Set the stage title
[Link](scene); // Place the scene in the stage
[Link](); // 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);
}
}
Exercise 7
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
12
13
import [Link];
import [Link];
import [Link];
public class ScrollBarDemo extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Text text = new Text(20, 20, "JavaFX Programming");
ScrollBar sbHorizontal = new ScrollBar();
ScrollBar sbVertical = new ScrollBar();
[Link]([Link]);
// Create a text in a pane
Pane paneForText = new Pane();
[Link]().add(text);
// Create a border pane to hold text and scroll bars
BorderPane pane = new BorderPane();
[Link](paneForText);
[Link](sbHorizontal);
[Link](sbVertical);
// Listener for horizontal scroll bar value change
[Link]().addListener(ov ->
[Link]([Link]() * [Link]() /
[Link]()));
13
14
// Listener for vertical scroll bar value change
[Link]().addListener(ov ->
[Link]([Link]() * [Link]() /
[Link]()));
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 450, 170);
[Link]("ScrollBarDemo"); // Set the stage title
[Link](scene); // Place the scene in the stage
[Link](); // 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);
}
}
Please see the Chapters: Event-Driven Programming and Animation, JavaFX UI
for above programming requirement and details
14