Question

In: Computer Science

Write a JavaFX application that displays a Label containing the opening sentence or two from your...

  1. Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a.

  2. Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b

// FXBookQuote1a.java

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.layout.Pane;

import javafx.scene.text.Font;

import javafx.stage.Stage;

public class FXBookQuote1a extends Application {

    //String storing the favorite quote from a book

    String quote = "Not All Those Who Wander Are Lost";

    @Override

    public void start(Stage primaryStage) {

        //creating a Label with the quote

        Label label=new Label(quote);

        //using a bigger font

        label.setFont(new Font(20));

        //creating a pane and adding the label

        Pane root = new Pane(label);

        //adding some padding space around the label

        label.setPadding(new Insets(50));

        //setting up a scene and displaying it

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("FXBookQuote1a");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

// FXBookQuote1b.java

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.scene.text.Font;

import javafx.stage.Stage;

public class FXBookQuote1b extends Application {

    //String storing the favorite quote from a book

    String quote = "Not All Those Who Wander Are Lost";

    //book name

    String bookName="Lord Of The Rings";

    @Override

    public void start(Stage primaryStage) {

        //creating a Label with the quote

        Label label1=new Label(quote);

        //using a bigger font

        label1.setFont(new Font(20));

       

        //creating another label to display book name, it is empty by default

        Label label2=new Label("");

       

        //creating a Button

        Button button=new Button("Show Book Name");

        //adding action listener to the button

        button.setOnAction(e->{

            //updating label2 with book name

            label2.setText(bookName);

        });

        //creating a VBox to arrange elements vertically, adding all elements

        VBox root = new VBox(label1,label2,button);

        root.setSpacing(10); //adding some space between components

        //aligning elements at center

        root.setAlignment(Pos.CENTER);

        //adding some space around the vbox

        root.setPadding(new Insets(30));

       

        //setting up a scene and displaying it

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("FXBookQuote1b");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

This Code is not working for me.

FXBookQuote1a.java:3: error: package javafx.application does not exist
import javafx.application.Application;
^

Solutions

Expert Solution

Since latest version of JDK does not support JavaFx. So there is one cheat. You can download an older version of JDK (jdk1.8).

After installing it, configure your IDE to old JDK.

like in below image. This may help you.

//Java code1

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.layout.Pane;

import javafx.scene.text.Font;

import javafx.stage.Stage;

public class FXBookQuote1a extends Application {

    //String storing the favorite quote from a book

    String quote = "Not All Those Who Wander Are Lost";

    @Override

    public void start(Stage primaryStage) {

        //creating a Label with the quote

        Label label=new Label(quote);

        //using a bigger font

        label.setFont(new Font(20));

        //creating a pane and adding the label

        Pane root = new Pane(label);

        //adding some padding space around the label

        label.setPadding(new Insets(50));

        //setting up a scene and displaying it

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("FXBookQuote1a");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

//Output

//Java Code2

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.layout.Pane;

import javafx.scene.layout.VBox;

import javafx.scene.text.Font;

import javafx.stage.Stage;

public class FXBookQuote1b extends Application {

    //String storing the favorite quote from a book

    String quote = "Not All Those Who Wander Are Lost";

    //book name

    String bookName="Lord Of The Rings";

    @Override

    public void start(Stage primaryStage) {

        //creating a Label with the quote

        Label label1=new Label(quote);

        //using a bigger font

        label1.setFont(new Font(20));



        //creating another label to display book name, it is empty by default

        Label label2=new Label("");



        //creating a Button

        Button button=new Button("Show Book Name");

        //adding action listener to the button

        button.setOnAction(e->{

            //updating label2 with book name

            label2.setText(bookName);

        });

        //creating a VBox to arrange elements vertically, adding all elements

        VBox root = new VBox(label1,label2,button);

        root.setSpacing(10); //adding some space between components

        //aligning elements at center

        root.setAlignment(Pos.CENTER);

        //adding some space around the vbox

        root.setPadding(new Insets(30));



        //setting up a scene and displaying it

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);

        primaryStage.setTitle("FXBookQuote1b");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

//output

//If you need any help regarding this solution .... please leave a comment ....... thanks


Related Solutions

Write a JavaFX application that presents two buttons and a number (initially 50) to the user....
Write a JavaFX application that presents two buttons and a number (initially 50) to the user. Label the buttons Increment and Decrement. When the increment button is pushed, increment the displayed value. Likewise, decrement the value when the decrement button is pushed. This has to use Java.Fx and not Java.Awt
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Part 1 Write a program that displays a frame containing two labels that display your name,...
Part 1 Write a program that displays a frame containing two labels that display your name, one for your first name and one for your last. Experiment with the size of the window to see the labels change their orientation to each other. USE FIRST NAME: Aya LAST NAME: KAYED. SEND THE CODE IN THE EXACT FORMAT FOR JAVA ECLIPSE. I WILL COPY AND PASTE THE CODE SO I CAN RUN IT. Part 2 Propose and solve your own digital...
Write a JavaFX multiple stage application which has at least two stages, a primary one and a secondary one.
JavaFX Two-Stage ApplicationWrite a JavaFX multiple stage application which has at least two stages, a primary one and a secondary one.The primary stage should have a gridpane which has at least a 2*3 grid.On each cell of the grid, please place a different node from at least one of the three types: a UI control or a shape or an image view, etc.On the secondary stage, you should design a layout with binding property.Your overall project design should reflect a...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.
Write an application named OddNums that displays all the odd numbers from 1 through 99. All...
Write an application named OddNums that displays all the odd numbers from 1 through 99. All ODD numbers should show all together(Should not need to scroll down), do it in 10 rows. FOR C#
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with the double click. Also, provide a button that clears the window and allows the user to begin again.
write a graphical application that displays a checkerboard with 25 squares.(black and white only and run...
write a graphical application that displays a checkerboard with 25 squares.(black and white only and run from pycharm.
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:         ● Subtotal using a cost of $3.50 per yogurt.         ● Subtotal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT