In Java
Write a JavaFX application that allows the user to pick a set of pizza toppings using a set of check boxes. Assuming each topping cost 50 cents, and a plain pizza costs $10, display the cost of the pizza.Us an Hbox
thank you!
Code
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;;
public class PizzaOrder extends Application {
@Override
public void start(Stage stage)
{
CheckBox chkPepperoni = new CheckBox("Pepperoni");
CheckBox chkCheez = new CheckBox("Extra Cheez");
CheckBox chkBacon = new CheckBox("Bacon");
CheckBox chkBlackOlives = new CheckBox("Black olives");
Label price = new Label("Total Cost: ");
//Creating the stop button
Button calculate = new Button("Place Order");
//Instantiating the HBox class
HBox hbox = new HBox();
//Setting the space between the nodes of a HBox pane
hbox.setSpacing(10);
//Setting the margin to the nodes
hbox.setMargin(chkPepperoni, new Insets(10, 10, 10, 10));
hbox.setMargin(chkCheez, new Insets(10,10,10,10));
hbox.setMargin(chkBacon, new Insets(10,10,10,10));
hbox.setMargin(chkBlackOlives, new Insets(10,10,10,10));
hbox.setMargin(price, new Insets(10, 10, 10, 10));
hbox.setMargin(calculate, new Insets(10, 10, 10, 10));
//retrieving the observable list of the HBox
ObservableList list = hbox.getChildren();
//Adding all the nodes to the observable list (HBox)
list.addAll(chkPepperoni,chkCheez,chkBacon,chkBlackOlives,calculate,price);
calculate.setOnAction((event) -> {
double pizzaPrice=10.00;
if(chkBacon.isSelected())
{
pizzaPrice+=0.50;
}
if(chkPepperoni.isSelected())
{
pizzaPrice+=0.50;
}
if(chkCheez.isSelected())
{
pizzaPrice+=0.50;
}
if(chkBlackOlives.isSelected())
{
pizzaPrice+=0.50;
}
price.setText("Total Cost: $"+pizzaPrice);
});
//Creating a scene object
Scene scene = new Scene(hbox);
//Setting title to the Stage
stage.setTitle("Pizza Order");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.