What is incorrect? I can\'t run my code. import javafx.application.Application;
ID: 3595707 • Letter: W
Question
What is incorrect? I can't run my code. import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javax.swing.*; public class RetailPriceCalculator extends Application implements EventHandler<ActionEvent> { TextField textWholesale; TextField textMarkup; Button btn; @Override public void start(Stage primaryStage) throws Exception { HBox root = new HBox(); root.setSpacing(10); root.setPadding(new Insets(15,20,10,10)); Label lbl1 = new Label("Enter the wholesale cost"); root.getChildren().add(lbl1); textWholesale = new TextField(""); textWholesale.setPrefWidth(110); root.getChildren().add(textWholesale); Label lbl2 = new Label("Enter the markup cost"); root.getChildren().add(lbl2); textMarkup = new TextField(""); textMarkup.setPrefWidth(110); root.getChildren().add(textWholesale); btn = new Button("calculate retail Price"); root.getChildren().add(btn); Scene scene = new Scene(root,550,250); primaryStage.setTitle("Retai Price calculator"); primaryStage.setScene(scene); primaryStage.show(); } public void handle(ActionEvent event) { double wholesaleCost, markupPct, retail; wholesaleCost = Double.parseDouble(textWholesale.getText()); markupPct = Double.parseDouble(textMarkup.getText()); retail = wholesaleCost + (wholesaleCost * markupPct); Alert alert = new Alert(Alert.AlertType.INFORMATION); if(event.getSource() == btn) { alert.setTitle("Message Box"); alert.setHeaderText(""); alert.setContentText(String.format("The Retail Price is %$",+retail)); alert.showAndWait(); } } public static void main(String[] args) { launch(args); } }
Explanation / Answer
The error found in the last line(In bold font) of the following code
Label lbl2 = new Label("Enter the markup cost");
root.getChildren().add(lbl2);
textMarkup = new TextField("");
textMarkup.setPrefWidth(110);
root.getChildren().add(textWholesale);
In the above line(bold) you are adding the input element "textWholesale", which is already added. You need to add "textMarkup ". rater than "textWholesale". Replace that line(shown in bold font) with the line below
root.getChildren().add(textMarkup);