Question: 1. Add MyJavaFX.java to your project. Modify the code to make the initial scene twice bigger. Change the title to MyBiggerJavaFX. import javafx.application.Application; import javafx.scene.Scene;
1. Add MyJavaFX.java to your project. Modify the code to make the initial scene twice bigger. Change the title to MyBiggerJavaFX.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage;
public class MyJavaFX 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"); 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); } }
2. Add MultipleStageDemo.java to your project. Modify the code to make the thrid window with title Third Stage. Make the second stage resizable but the third stage un-resizable.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage;
public class MultipleStageDemo extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place a button in the scene Scene scene = new Scene(new Button("OK"), 200, 250); primaryStage.setTitle("MyJavaFX"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage
Stage stage = new Stage(); // Create a new stage stage.setTitle("Second Stage"); // Set the stage title // Set a scene with a button in the stage stage.setScene(new Scene(new Button("New Stage"), 100, 100)); stage.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); } }
3. Add ButtonInPane.java to your project. Run it. Observe the output. Both MyJavaFX.java and the current program displayed a button in the window, but the latter button keeps its own size. Which type of container enabled this feature?
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage;
public class ButtonInPane extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a scene and place a button in the scene StackPane pane = new StackPane(); pane.getChildren().add(new Button("OK")); Scene scene = new Scene(pane, 200, 50); primaryStage.setTitle("Button in a pane"); // 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); } }
4.
Add ShowCircle.java and ShowCricleCentered.java to your project. Run each of them and then resize the windows to see the difference. Modify the ShowCricleCentered.java to make circle bound to the right boundary of the window, no matter how you resize the window (as figure A shows).
Optional: Modify the ShowCricleCentered.java to make circle bound to the left boundary of the window, no matter how you resize the window (as figure B shows).
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage;
public class ShowCircle extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { // Create a circle and set its properties Circle circle = new Circle(); circle.setCenterX(100); circle.setCenterY(100); circle.setRadius(50); circle.setStroke(Color.BLACK); circle.setFill(null); // Create a pane to hold the circle Pane pane = new Pane(); pane.getChildren().add(circle); // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 200); primaryStage.setTitle("ShowCircle"); // 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); } }
5.
Add BindingDemo.java to your project. The program has an error. Try to fix the problem. Hint: you cannot change the value of a target after the binding, since it is already bound to the source.
If we change the unidirectional binding to bidirectional binding, we will be able to change the value of both d1 and d2. Reload the original BindingDemo.java, change the bind method to bindBidirectional. Run the program again. There should be no error.
import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty;
public class BindingDemo { public static void main(String[] args) { DoubleProperty d1 = new SimpleDoubleProperty(1); DoubleProperty d2 = new SimpleDoubleProperty(2); d1.bind(d2); System.out.println("d1 is " + d1.getValue() + " and d2 is " + d2.getValue()); d1.setValue(50.2); System.out.println("d1 is " + d1.getValue() + " and d2 is " + d2.getValue()); d2.setValue(70.2); System.out.println("d1 is " + d1.getValue() + " and d2 is " + d2.getValue()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
