import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MPGCalc extends Application {
private TextField tfMiles = new TextField();
private TextField tfGallons = new TextField();
private TextField tfMiles1 = new TextField();
private TextField tfGallons1 = new TextField();
private TextField tfMiles2 = new TextField();
private TextField tfGallons2 = new TextField();
private TextField tfMiles3 = new TextField();
private TextField tfGallons3 = new TextField();
private TextField tfMiles4 = new TextField();
private TextField tfGallons4 = new TextField();
private Button calcMPG = new Button("Calculate");
private TextField displayMPG = new TextField();
private RadioButton rbStandard = new RadioButton("Standard");
private RadioButton rbMetric = new RadioButton("Metric");
private Label lblDistance = new Label("Miles:");
private Label lblAmount = new Label("Gallons:");
private Label lblDistance1 = new Label("Miles:");
private Label lblAmount1 = new Label("Gallons:");
private Label lblDistance2 = new Label("Miles:");
private Label lblAmount2 = new Label("Gallons:");
private Label lblDistance3 = new Label("Miles:");
private Label lblAmount3 = new Label("Gallons:");
private Label lblDistance4 = new Label("Miles:");
private Label lblAmount4 = new Label("Gallons:");
private Label lblMPG = new Label("MPG:");
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create UI
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(rbStandard, 0 ,0);
gridPane.add(rbMetric, 1, 0);
gridPane.add(lblDistance, 0, 1);
gridPane.add(tfMiles, 1, 1);
gridPane.add(lblAmount, 0, 2);
gridPane.add(tfGallons, 1, 2);
gridPane.add(lblDistance1, 0, 3);
gridPane.add(tfMiles1, 1, 3);
gridPane.add(lblAmount1, 0, 4);
gridPane.add(tfGallons1, 1, 4);
gridPane.add(lblDistance2, 0, 5);
gridPane.add(tfMiles2, 1, 5);
gridPane.add(lblAmount2, 0, 6);
gridPane.add(tfGallons2, 1, 6);
gridPane.add(lblDistance3, 0, 7);
gridPane.add(tfMiles3, 1, 7);
gridPane.add(lblAmount3, 0, 8);
gridPane.add(tfGallons3, 1, 8);
gridPane.add(lblDistance4, 0, 9);
gridPane.add(tfMiles4, 1, 9);
gridPane.add(lblAmount4, 0, 10);
gridPane.add(tfGallons4, 1, 10);
gridPane.add(calcMPG, 0, 11);
gridPane.add(displayMPG, 1, 12);
gridPane.add(lblMPG, 0, 12);
// Set properties for UI
gridPane.setAlignment(Pos.CENTER);
tfMiles.setAlignment(Pos.BOTTOM_RIGHT);
tfGallons.setAlignment(Pos.BOTTOM_RIGHT);
calcMPG.setAlignment(Pos.BOTTOM_RIGHT);
displayMPG.setAlignment(Pos.BOTTOM_RIGHT);
displayMPG.setEditable(false);
ToggleGroup group = new ToggleGroup();
rbStandard.setToggleGroup(group);
rbMetric.setToggleGroup(group);
// Process events
calcMPG.setOnAction(e -> calculateMPG());
rbStandard.setOnAction(e -> rbStandard());
rbMetric.setOnAction(e -> rbMetric());
// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 400, 250);
primaryStage.setTitle("MPG Calculator"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
rbStandard.setSelected(true);
}
private void calculateMPG() {
double gallons = Double.parseDouble(tfGallons.getText());
double miles = Double.parseDouble(tfMiles.getText());
double mpg = miles / gallons;
displayMPG.setText(String.format("%.2f", mpg));
}
private void rbStandard(){
lblDistance.setText("Miles:");
lblAmount.setText("Gallons:");
lblMPG.setText("MPG:");
double liters = Double.parseDouble(tfGallons.getText());
double kilometers = Double.parseDouble(tfMiles.getText());
double kml = Double.parseDouble(tfMiles.getText());
double gallons = liters / 3.785411784;
double miles = kilometers * 1.6;
double mpg = miles / liters;
tfGallons.setText(String.format("%.2f", gallons));
tfMiles.setText(String.format("%.2f", miles));
displayMPG.setText(String.format("%.2f", mpg));
}
private void rbMetric(){
lblDistance.setText("Kilometers:");
lblAmount.setText("Liters:");
lblMPG.setText("KM/L:");
double gallons = Double.parseDouble(tfGallons.getText());
double miles = Double.parseDouble(tfMiles.getText());
double mpg = Double.parseDouble(tfMiles.getText());
double liters = gallons * 3.785411784;
double kilometers = miles/1.6;
double kml = miles / liters;
tfGallons.setText(String.format("@%.2f", gallons));
tfMiles.setText(String.format("@%.2f", miles));
displayMPG.setText(String.format("@%.2f", mpg));
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
这开始只是一个MPG计算器,但现在我试图让它计算行程中最多5个停止的MPG,然后在5个停止点结束时,计算行程的最终MPG。
现在它将计算第一站的MPG,但之后它会遗漏文本字段1-4的输入。
我尝试将calculateMPG中的getText设置为正确的文本字段,但它仍然没有更改输出。我还考虑过为每条腿制作几种方法,但我不确定是否有更有效的方法。
答案 0 :(得分:0)
如果您想要
,每个人都可以自由编辑以提高效率,这是一个更好的解决方案import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class MPGCalc extends Application {
private Button calcMPG = new Button("Calculate");
private TextField displayMPG = new TextField();
private RadioButton rbStandard = new RadioButton("Standard");
private RadioButton rbMetric = new RadioButton("Metric");
private Label[] distanceLabels = new Label[5];
private Label[] amountLabels = new Label[5];
private TextField[] distanceTextFields = new TextField[5];
private TextField[] amountTextFields = new TextField[5];
private Label lblMPG = new Label("MPG:");
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
createLists();
// Create UI
GridPane gridPane = new GridPane();
gridPane.setHgap(5);
gridPane.setVgap(5);
gridPane.add(rbStandard, 0 ,0);
gridPane.add(rbMetric, 1, 0);
for (int i = 0; i < 10; i++) {
if(i%2==0) {
gridPane.add(distanceLabels[i / 2], 0, i+1);
gridPane.add(distanceTextFields[i / 2], 1, i+1);
}
else {
gridPane.add(amountLabels[i / 2], 0, i+1);
gridPane.add(amountTextFields[i / 2], 1, i+1);
}
}
gridPane.add(calcMPG, 0, 11);
gridPane.add(displayMPG, 1, 12);
gridPane.add(lblMPG, 0, 12);
// Set properties for UI
gridPane.setAlignment(Pos.CENTER);
calcMPG.setAlignment(Pos.BOTTOM_RIGHT);
for (int i = 0; i < distanceLabels.length; i++) {
distanceLabels[i].setAlignment(Pos.BOTTOM_RIGHT);
distanceTextFields[i].setAlignment(Pos.BOTTOM_RIGHT);
}
for (int i = 0; i < amountLabels.length; i++) {
amountLabels[i].setAlignment(Pos.BOTTOM_RIGHT);
amountTextFields[i].setAlignment(Pos.BOTTOM_RIGHT);
}
displayMPG.setAlignment(Pos.BOTTOM_RIGHT);
displayMPG.setEditable(false);
ToggleGroup group = new ToggleGroup();
rbStandard.setToggleGroup(group);
rbMetric.setToggleGroup(group);
// Process events
calcMPG.setOnAction(e -> calculateMPG());
rbStandard.setOnAction(e -> rbStandard());
rbMetric.setOnAction(e -> rbMetric());
// Create a scene and place it in the stage
Scene scene = new Scene(gridPane, 400, 425);
primaryStage.setTitle("MPG Calculator"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
rbStandard.setSelected(true);
}
private void createLists(){
for (int i = 0; i < distanceLabels.length; i++) {
distanceLabels[i] = new Label("Miles");
distanceTextFields[i] = new TextField("");
}
for (int i = 0; i < amountLabels.length; i++) {
amountLabels[i] = new Label("Gallons:");
amountTextFields[i] = new TextField("");
}
}
private void setDistanceLabel(String labelName){
for (Label distanceLabel : distanceLabels)
distanceLabel.setText(labelName);
}
private void setAmountLabels(String labelName){
for (Label amountLabel : amountLabels)
amountLabel.setText(labelName);
}
private void calculateMPG() {
double totalTrip = 0;
for (TextField distanceTextField : distanceTextFields)
if (distanceTextField != null && !distanceTextField.getText().equals(""))
totalTrip = totalTrip + Double.parseDouble(distanceTextField.getText());
double totalAmount = 0;
for (TextField amountTextField : amountTextFields) {
if (amountTextField != null && !amountTextField.getText().equals(""))
totalAmount = totalAmount +Double.parseDouble(amountTextField.getText());
}
displayMPG.setText(String.valueOf(totalTrip/totalAmount));
}
private void rbStandard(){
setDistanceLabel("Miles");
setAmountLabels("Gallons:");
lblMPG.setText("MPG:");
}
private void rbMetric(){
setDistanceLabel("Kilometers:");
setAmountLabels("Liters:");
lblMPG.setText("KM/L:");
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}