我不确定如何在不同的类中给出这些值以在 Javafx 中为我正在制作的 GUI 相互连接

  • 本文关键字:Javafx 连接 GUI 不确定 java javafx
  • 更新时间 :
  • 英文 :


这是Javafx,我正在制作一个包含多个类的GUI。

代码:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class AutoLoanCalculator extends Application{
public static void main(String[] args) {
launch(args);
}


public void start(Stage primaryStage) {


PaymentSection section1 = new PaymentSection();
LoanTermSection section2 = new LoanTermSection();
FinancingSection section3 = new FinancingSection();
OptionsSection section4 = new OptionsSection();

Button calc = new Button("Calculate");
Button reset = new Button("Reset");
Button exit = new Button("Exit");

GridPane comboGrid = new GridPane();
comboGrid.add(section1, 0, 0);
comboGrid.add(section2, 1, 0);
comboGrid.add(section3, 0, 1);
comboGrid.add(section4, 1, 1);
comboGrid.add(calc, 0,2);
comboGrid.add(reset, 1,2);
comboGrid.add(exit, 2,2);

HBox combo = new HBox();
combo.getChildren().add(comboGrid);

calc.setOnAction(event ->{

Double basePrice = Double.parseDouble(section3.getPriceNum());
Double downPayment = Double.parseDouble(section3.getDownPayNum());
Double taxRate = Double.parseDouble(section3.getTaxNum()) / 100;
Double optionPrice = section4.getCost();
Double interestRate = section2.getRate() / 100;
int loanTermMonths = section2.getMonth();

//calculates the values of rate and total sales tax
double rate = interestRate/12;
double salesTaxAmt = (basePrice - downPayment + optionPrice) * taxRate;

Double totalLoanAmt = basePrice - downPayment + optionPrice + salesTaxAmt;
Double monthPayment = totalLoanAmt * (rate * Math.pow(1 + rate, loanTermMonths)) / (Math.pow(1 + rate, loanTermMonths) - 1);
Double totalPayment = monthPayment * loanTermMonths + downPayment;

//section1.setLoanNum(String.format("%.2f", totalLoanAmt));

System.out.println(basePrice);
System.out.println(downPayment);
System.out.println(taxRate);
});

reset.setOnAction(event ->{
section1.reset();
section2.reset();
section3.reset();
section4.reset();
});

exit.setOnAction(event ->{
primaryStage.close();
});

Scene scene = new Scene(combo);
primaryStage.setTitle("Auto Loan Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
}

//我把这个部分称为

import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
public class PaymentSection extends GridPane{
private Label payInfo;
private Label loanAmount;
private Label monthlyPayment;
private Label totalPayment;
private Label loanNum;
private Label monthNum;
private Label totalNum;
private String loanInt;
private String monthInt;
private String totalInt;

public PaymentSection () {
payInfo = new Label("Payment Information");
loanAmount = new Label("Total Loan Amount: $ ");
monthlyPayment = new Label("Monthly Payment: $ ");
totalPayment = new Label("Total Payment: $ ");

loanNum = new Label();
monthNum = new Label();
totalNum = new Label();



loanNum.setText("0.0");
monthNum.setText("0.0");
totalNum.setText("0.0");


add(payInfo,0,0);
add(loanAmount,0,1);
add(monthlyPayment,0,2);
add(totalPayment,0,3);
add(loanNum,1,1);
add(monthNum,1,2);
add(totalNum,1,3);
setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" + 
"-fx-border-width: 2;" +
"-fx-border-color: black;");
}

public String setLoanNum(String string) {
return loanInt;
}

public void reset() {
loanNum.setText("0.0");
monthNum.setText("0.0");
totalNum.setText("0.0");
}

}

//我把这个部分叫做

import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class FinancingSection extends GridPane{
private Label finance;
private Label price;
private Label tax;
private Label downPay;
private TextField priceNum;
private TextField taxNum;
private TextField downPayNum;
private String priceInt;
private String downPayInt;
private String taxInt;

public FinancingSection() {

LoanTermSection term = new LoanTermSection();
term.getRate();

finance = new Label("Fianancing Information");
price = new Label("Base Price: $ ");
downPay = new Label("Down Payment: $ ");
tax = new Label("Sales Tax: % ");

priceNum = new TextField("0.0");
downPayNum = new TextField("0.0");
taxNum = new TextField("7.0");

priceInt = priceNum.getText();
downPayInt = downPayNum.getText();
taxInt = taxNum.getText();

add(finance,0,0);
add(price,0,1);
add(downPay,0,2);
add(tax,0,3);
add(priceNum,1,1);
add(downPayNum,1,2);
add(taxNum,1,3);
setStyle("-fx-padding: 10;" +
"-fx-border-style: solid inside;" + 
"-fx-border-width: 2;" +
"-fx-border-color: black;");


}

public String getPriceNum() {
return priceInt;
}
public void setPriceNum(String newPriceNum) {
priceInt = newPriceNum;
}

public String getDownPayNum() {
return downPayInt;
}
public void setDownPayNum(String newDownPay) {
downPayInt = newDownPay;
}

public String getTaxNum() {
return taxInt;
}
public void setTaxNum(String newTax) {
taxInt = newTax;
}

public void reset() {
priceNum.setText("0.0");
downPayNum.setText("0.0");
taxNum.setText("7.0");
}

}

问题是,我无法设置basePrice、downPayment和taxRate-我无法从第3节代码中设置的值中获取它们:我曾想过使用setter和getter,但似乎不起作用,我不确定为什么或如何修复它。-也许这与我将它们设置为的值有关-真的不确定。至于第1节,我无法将其打印出来——我认为这与第3节的问题相同。因此,我主要只需要帮助获取第3节的值。提前感谢!

在检索值时,需要对文本字段调用getText()。例如,

public class FinancingSection extends GridPane{
// ...
public String getPriceNum() {
return priceNum.getText();
}
// ...
}

您可能可以去掉priceInt变量等,并且可能应该去掉FinancingSection中未使用的set方法。

PaymentSection中,您正在使用的集合方法应该更新其相应的标签,例如

public class PaymentSection extends GridPane{
// ...
public void setLoanNum(String string) {
loanNum.setText(string) ;
}
// ...
}

最新更新