我不确定如何让这些值在不同的 类 中给出,以便在我正在制作的 GUI 中在 Javafx 中相互连接
I'm not sure how to get these values given in different classes to connect to each other in Javafx for a GUI I'm making
这是 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();
}
}
//我称这个部分为1
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");
}
}
//我称这个部分为3
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
中设置的方法没有使用。
您在 PaymentSection
中使用的设置方法应更新其相应的标签,例如
public class PaymentSection extends GridPane{
// ...
public void setLoanNum(String string) {
loanNum.setText(string) ;
}
// ...
}
这是 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();
}
}
//我称这个部分为1
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");
}
}
//我称这个部分为3
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
中设置的方法没有使用。
您在 PaymentSection
中使用的设置方法应更新其相应的标签,例如
public class PaymentSection extends GridPane{
// ...
public void setLoanNum(String string) {
loanNum.setText(string) ;
}
// ...
}