In: Computer Science
Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a.
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;
^
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