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

Java Write a JavaFX application that displays a button and a number. Every time the button...
Java Write a JavaFX application that displays a button and a number. Every time the button is pushed, change the number to a random value between 1 and 100. Thank you and can you show a picture of the result as well
Write a program that displays a frame containing two labels that display your name, one for...
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.
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 application that presents a button and a circle. Every time the button is...
Write a JavaFX application that presents a button and a circle. Every time the button is pushed, the circle should be moved to a new random location within the window. This must only be one .java file. I cannot use import javafx.geometry.Insets; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.HBox; This is what I have so far: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.shape.Circle; import javafx.scene.paint.Color; import javafx.stage.Stage; public class CircleJumper extends Application...
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
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#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT