Javafx应用程序不会仅运行构建而构建会失败

时间:2019-02-14 03:26:45

标签: java javafx

我目前正在开发一个应用程序,该应用程序计算贷款的每月还款额。香港专业教育学院从我的其他程序中获取代码,使我想要的东西,我相信我拥有执行它所需的一切,但是构建失败,我不确定为什么。 Javafx是我的新手,所以我很想了解更多有关Javafx的信息,但可以对我的错误使用一些指导

我尝试过与以前的程序逐行处理,但是看不到任何差异,因此不确定为什么会出错。也有一个同学看着它来帮助调试

package project2a;
import javafx.application.Application;  
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 *
 * @author Carmine , Eric , Rohit 
 */
public class Project2a extends Application {


//Text fields for loan amount
TextField tfLoanAmount = new TextField();
TextField tfInterestRate= new TextField();
TextArea textArea = new TextArea();

//Radio buttons for years
private RadioButton rbOneYear;
private RadioButton rbTwoYear;
private RadioButton rbFiveYear;
private RadioButton rbSevenYear;
private RadioButton rbTenYear;
private RadioButton rbFifteenYear;
private ToggleGroup group;

private Button btnCalculate;

//labels for output
 private Label lblMonthlyPayment;
 private Label lblHeader;
 private Label lblYears;

 //vbox for layout of items
 private VBox vBoxRadio;
 private VBox vBoxInputs;
 private VBox vBoxResults;

 private TitledPane titledPaneRadio;

 @Override
 public void start(Stage primaryStage)
 {
     //makes header
     lblHeader= new Label("Calculate your monthly loan payment!");
     lblHeader.setMinWidth(350);
     lblHeader.setAlignment(Pos.CENTER);
     lblHeader.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 28px; -fx-text-fill: white; -fx-background-color: rgb(104, 50, 0); -fx-font-weight: bold;");


     EventHandler<ActionEvent> handler = event -> updateRates();
    tfLoanAmount.setOnAction(handler);
    tfInterestRate.setOnAction(handler);
    TextField tfLoanAmount = new TextField();


     rbOneYear= new RadioButton("One Years");
     rbTwoYear= new RadioButton("Two Years");
     rbFiveYear= new RadioButton("Five Years");
     rbSevenYear= new RadioButton("Seven Years");
     rbTenYear= new RadioButton("Ten Years");
     rbFifteenYear= new RadioButton("Fifteen Years");

     group = new ToggleGroup();
     group.getToggles().addAll(rbOneYear, rbTwoYear, rbFiveYear, rbSevenYear, rbTenYear, rbFifteenYear);

    rbFiveYear.setSelected(true);

    vBoxRadio = new VBox(20,rbOneYear, rbTwoYear, rbFiveYear, rbSevenYear, rbTenYear, rbFifteenYear);
    vBoxRadio.setPadding( new Insets(10) );

    titledPaneRadio = new TitledPane("Number of Years", vBoxRadio);

            vBoxInputs = new VBox(0, titledPaneRadio);



    //creates calculate button
    btnCalculate = new Button("    Calculate    ");
    btnCalculate.setStyle("-fx-text-fill: white; -fx-background-color: tan; -fx-font-weight: bold;");
    btnCalculate.setOnAction( e -> calculateMonthlyPayment(e) );

    lblYears= new Label("Number of Years:  \t");



    vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);
    vBoxResults.setPrefWidth(140);
    vBoxResults.setPrefHeight(270);
    vBoxResults.setPadding( new Insets(10) );
    vBoxResults.setStyle("-fx-background-color: cornsilk; -fx-border-color: black; -fx-border-radius: 10;");


    GridPane grid = new GridPane();

    grid.addRow(0, lblHeader);
    GridPane.setColumnSpan(lblHeader, 2);
    GridPane.setHalignment(lblHeader, HPos.CENTER);

    grid.addRow(1, vBoxInputs, vBoxResults);

    Scene scene = new Scene(grid, 350, 310);

    primaryStage.setTitle("Loan Payment Calculator");
    primaryStage.setResizable(false);
    primaryStage.setScene(scene);
    primaryStage.show();

 }

 private void updateRates() {
    double annualInterestRate = 5.00;

    double loanAmount = Double.parseDouble(tfLoanAmount.getText());

    double numberOfYears = Double.parseDouble(lblYears.getText());

    String s = String.format("%-1s%20s%20s\n", "Interest Rate", "Monthly Payment", "Total Payment");
    // making loop to display different interest rates
    for ( ; annualInterestRate <= 8.00; annualInterestRate += 0.125) {

        // calculating monthly and total payment rates
        double monthlyInterestRate = annualInterestRate / 1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

        double totalPayment = monthlyPayment * numberOfYears * 12;

        // making % string for formatting reasons...
        String str = "%";

        // Displaying formatted info
        s += String.format("%-1.3f%s%19.2f%30.2f \n", annualInterestRate, str, ((int) (monthlyPayment * 100) / 100.0), ((int) (totalPayment * 100) / 100.0));
    }

    textArea.setText(s);
}

 private void calculateMonthlyPayment(ActionEvent e)
 {
     int years=0;

     if(rbOneYear.isSelected())
     {
         years=1;
     }

     else if(rbTwoYear.isSelected())
     {
         years=2;
     }

     else if(rbFiveYear.isSelected())
     {
         years=5;
     }

     else if (rbSevenYear.isSelected())
     {
         years=7;
     }

     else if(rbTenYear.isSelected())
     {
         years=10;
     }

     else if(rbTenYear.isSelected())
     {
         years=10;
     }

     lblYears.setText("Year(s) Selected: " + years);
     lblMonthlyPayment.setText("Monthly Payment: "  );
 }


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

预计会有6个单选按钮来表示贷款年限,侧面窗格中的标签中显示的年份,用于输入贷款额和利率的文本字段供用户输入,并显示在侧面面板以及用于计算。还有一些颜色等使用户看起来不错

1 个答案:

答案 0 :(得分:1)

vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);中引发异常
您的问题是将未初始化的(null)lblMonthlyPayment添加到VBox中, 因此,初始化lblMonthlyPayment可以解决您的应用,并且可以正常运行

请参阅下面

lblYears = new Label("Number of Years:  \t");
lblMonthlyPayment =  new Label("");

vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);

enter image description here