(Its an app) (Miles-per-Gallon Calculator App) In JAVA with comments Open Netbea
ID: 3720490 • Letter: #
Question
(Its an app) (Miles-per-Gallon Calculator App) In JAVA with comments
Open Netbeans IDE
Go to file > New project > JAVAFX > JAVAFX > FXML Application
Set required properties for the project
open<filename>.fxml in JavaFX Screen Builder
Place AnchorPane in design and set its attributes
Place mileTextField, gallonsTextField and calcButton on the AnchorPane and set their attributes
Place label that will show the calculated value and set its attributes
Set the controller function on the left side of JavaFX screen Builder and asign TextFields, label, and button with their respective fx:id's
Drivers often want to know the miles per gallon their cars get so they can estimate gasoline costs. Develop an app that allows the user to input the number of miles driven and the number of gallons used and calculates and displays the corresponding miles per gallon.
Explanation / Answer
Miles-per-Gallon Calculator App
Below is the code for javafx of miles per gallon app. It calculates MPG using user input of miles and gallons.
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mpg.FXMLDocumentController">
<children>
<Label text="Enter Miles:" layoutX="20" layoutY="50" minHeight="16" minWidth="69" fx:id="miles" />
<TextField layoutX="100" layoutY="50" minHeight="16" minWidth="69" fx:id="mileTextField" />
<Label text="Enter Gallons:" layoutX="20" layoutY="90" minHeight="16" minWidth="69" fx:id="gallons" />
<TextField layoutX="100" layoutY="90" minHeight="16" minWidth="69" fx:id="gallonsTextField" />
<Button layoutX="126" layoutY="120" text="Calculate" fx:id="calculate" />
<Label layoutX="100" layoutY="160" minHeight="16" minWidth="69" fx:id="gpm" />
</children>
</AnchorPane>
FXMLDocumentController.java
package mpg;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button button;
@FXML
private TextField mileTextField;
@FXML
private TextField gallonsTextField;
@FXML
private Label gpm;
@FXML
private void handleButtonAction(ActionEvent event) {
double miles;
double gallons;
double mpg;
miles = Double.parseDouble(mileTextField.getText());
gallons = Double.parseDouble(gallonsTextField.getText());
DecimalFormat formatter = new DecimalFormat("#0.00");
mpg = miles/gallons;
gpm.setText("Miles Per Gallon:"+formatter.format(mpg));
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
MilesPerGallon.java
package mpg;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MilesPerGallon extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.