Question: Question: Modify the QuoteOptions program from Chapter 5 so that it provides three additional quote options. Use arrays to store the category options and quotes,
Question: Modify the QuoteOptions program from Chapter 5 so that it provides three additional quote options. Use arrays to store the category options and quotes, and a choice box to present the options (instead of radio buttons).
Please use the same format as these source codes.
Source Code 1:
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.stage.Stage;
//************************************************************************ // QuoteOptions.java Author: Lewis/Loftus // // Demonstrates the use of radio buttons. //************************************************************************
public class QuoteOptions extends Application { //-------------------------------------------------------------------- // Creates and presents the program window. //-------------------------------------------------------------------- public void start(Stage primaryStage) { QuoteOptionsPane pane = new QuoteOptionsPane(); pane.setAlignment(Pos.CENTER); pane.setStyle("-fx-background-color: lightgreen");
Scene scene = new Scene(pane, 500, 150); primaryStage.setTitle("Quote Options"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Source Code 2:
import javafx.event.ActionEvent; import javafx.geometry.Pos; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; import javafx.scene.text.Font;
//************************************************************************ // QuoteOptionsPane.java Author: Lewis/Loftus // // Demonstrates the use of radio buttons. //************************************************************************
public class QuoteOptionsPane extends HBox { private Text quote; private String philosophyQuote, carpentryQuote, comedyQuote; private RadioButton philosophyButton, carpentryButton, comedyButton;
//-------------------------------------------------------------------- // Sets up this pane with a Text object and radio buttons that // determine which phrase is displayed. //-------------------------------------------------------------------- public QuoteOptionsPane() { philosophyQuote = "I think, therefore I am."; carpentryQuote = "Measure twice. Cut once."; comedyQuote = "Take my wife, please."; quote = new Text(philosophyQuote); quote.setFont(new Font("Helvetica", 24)); StackPane quotePane = new StackPane(quote); quotePane.setPrefSize(300, 100); ToggleGroup group = new ToggleGroup(); philosophyButton = new RadioButton("Philosophy"); philosophyButton.setSelected(true); philosophyButton.setToggleGroup(group); philosophyButton.setOnAction(this::processRadioButtonAction); carpentryButton = new RadioButton("Carpentry"); carpentryButton.setToggleGroup(group); carpentryButton.setOnAction(this::processRadioButtonAction); comedyButton = new RadioButton("Comedy"); comedyButton.setToggleGroup(group); comedyButton.setOnAction(this::processRadioButtonAction); VBox options = new VBox(philosophyButton, carpentryButton, comedyButton); options.setAlignment(Pos.CENTER_LEFT); options.setSpacing(10); setSpacing(20); getChildren().addAll(options, quotePane); } //-------------------------------------------------------------------- // Updates the content of the displayed text. //-------------------------------------------------------------------- public void processRadioButtonAction(ActionEvent event) { if (philosophyButton.isSelected()) quote.setText(philosophyQuote); else if (carpentryButton.isSelected()) quote.setText(carpentryQuote); else quote.setText(comedyQuote); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
