*Last repost* *Sorry for the repost! Been a work in progress* Had to add 2 new s
ID: 3703713 • Letter: #
Question
*Last repost*
*Sorry for the repost! Been a work in progress*
Had to add 2 new steps after #3
Need to make a simple JavaFx GUI that will have:
1) 2 separate text fields for 2 prices to be entered (and displayed): ("plant cost" and "soil cost")
2) a button to be clicked that will calculate the average of the 2 prices
3) a text field that will display the average between 2 prices
NEW) underneath average cost, a button to 'Add Plant' to a new text box below it
THEN) that new text box that will list the plant AND it's average cost next to it. Must be able to list at least 3 plants at a time, along with their avg cost next to it
4) An 'exit' button that will close the GUI
Thanks so much!
Explanation / Answer
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.util.ArrayList
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.io.Serializable;
import javafx.scene.paint.Color;
public class CalculateAverage extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TO dO Auto-generated method stub
Label plantCost = new Label("Plant Cost");//Plant Cost label
TextField plant = new TextField("0");//textfield for plant cost
Label soilCost = new Label("Soil Cost");//Soil Cost label
TextField soil = new TextField("0");//textfield for soil Cost.
Button calcAvg = new Button("Calculte Average");//button clickable.
Label avg = new Label("Average");//Average label.
TextField average = new TextField("0");//textfield for average.
//hbox1
HBox hbox1 = new HBox();
hbox1.setLayoutX(50);
hbox1.setLayoutY(200);
hbox1.setMargin(plantCost, new Insets(10, 10, 50, 50));
hbox1.setMargin(plant, new Insets(10, 50, 50, 10));
ObservableList list1 = hbox1.getChildren();//an observable list is declared to add the elements(in this case it adds nodes to hbox1).
list1.addAll(plantCost, plant);//all the nodes are added to the list of type ObservableList.
//hbox2
HBox hbox2 = new HBox();
hbox2.setLayoutX(50);
hbox2.setLayoutY(250);
hbox2.setMargin(soilCost, new Insets(10, 10, 50, 50));
hbox2.setMargin(soil, new Insets(10, 50, 50, 20));
ObservableList list2 = hbox2.getChildren(); //an observable list is declared to add the elements(in this case it adds nodes to hbox2).
list2.addAll(soilCost, soil);//the nodes are added to the list of type ObservableList.
//hbox3
HBox hbox3 = new HBox();
hbox3.setLayoutX(150);
hbox3.setLayoutY(300);
hbox3.setMargin(calcAvg, new Insets(10, 10, 10, 50));
ObservableList list3 = hbox3.getChildren();
list3.addAll(calcAvg);
//hbox4
HBox hbox4 = new HBox();
hbox4.setLayoutX(50);
hbox4.setLayoutY(350);
hbox4.setMargin(avg, new Insets(10, 10, 10, 50));
hbox4.setMargin(average, new Insets(10, 50, 10, 19));
ObservableList list4 = hbox4.getChildren();
list4.addAll(avg, average);
//creating a Group object and added all the hbox layouts to it.
Group group = new Group(hbox1, hbox2, hbox3, hbox4);
//Creating a Scene by passing the group object, height and width
Scene scene = new Scene(group ,400, 500);
//event handler is set to calculate average button, when the button is clicked the code inside the block gets executed.
calcAvg.setOnMouseClicked((new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
//takes value from plant textfield.
double plant_cost = Double.parseDouble(plant.getText());
//takes value from soil textfield.
double soil_cost = Double.parseDouble(soil.getText());
double average_ps = (plant_cost+soil_cost)/2;//calculates average of the two and
average.setText(average_ps+"");//sets the average value in average textfield.
}
}));
//setting color to the scene
scene.setFill(Color.SILVER);
//Setting the title to Stage.
primaryStage.setTitle("Calculate Average");
//Adding the scene to Stage
primaryStage.setScene(scene);
//Displaying the contents of the stage
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.