我正在編寫一個程序,將放在文本框中的數字加起來。當按下按鈕時,輸出應該放在每行文本框末尾的文本框中。沒有任何事情發生。有什麼我失蹤?這是我的代碼。處理事件並添加每個文本框的方法在底部按鈕被按下時沒有任何反應javaFX
package taxerfxml;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.swing.JComboBox;
/**
*
*
*/
public class FXMLDocumentController implements Initializable {
ObservableList<String> comboBoxList = FXCollections.observableArrayList("Ccosta", "Alam", "Sclara", "Smateo", "Sf", "Marin", "Sonoma", "Napa", "Solono");
//fxml combo boxes
@FXML
private ComboBox comboBox1;
@FXML
private ComboBox comboBox2;
@FXML
private ComboBox comboBox3;
@FXML
private ComboBox comboBox4;
@FXML
private ComboBox comboBox5;
@FXML
private ComboBox comboBox6;
@FXML
private ComboBox comboBox7;
@FXML
private ComboBox comboBox8;
@FXML
private ComboBox comboBox9;
@FXML
private ComboBox comboBox10;
@FXML
private ComboBox comboBox11;
@FXML
private ComboBox comboBox12;
//fxml labels
@FXML
private Label sale;
@FXML
private Label tax;
@FXML
private Label county;
@FXML
private Label freight;
@FXML
private Label labor;
@FXML
private Label cost;
@FXML
private Label profit;
@FXML
private Label customer;
// fxml customer textfields
@FXML
private TextField customer1;
@FXML
private TextField customer2;
@FXML
private TextField customer3;
@FXML
private TextField customer4;
@FXML
private TextField customer5;
@FXML
private TextField customer6;
@FXML
private TextField customer7;
@FXML
private TextField customer8;
@FXML
private TextField customer9;
@FXML
private TextField customer10;
@FXML
private TextField customer11;
@FXML
private TextField customer12;
//fxml sale textfields
@FXML
private TextField sale1;
@FXML
private TextField sale2;
@FXML
private TextField sale3;
@FXML
private TextField sale4;
@FXML
private TextField sale5;
@FXML
private TextField sale6;
@FXML
private TextField sale7;
@FXML
private TextField sale8;
@FXML
private TextField sale9;
@FXML
private TextField sale10;
@FXML
private TextField sale11;
@FXML
private TextField sale12;
//fxml tax textfields
@FXML
private TextField tax1;
@FXML
private TextField tax2;
@FXML
private TextField tax3;
@FXML
private TextField tax4;
@FXML
private TextField tax5;
@FXML
private TextField tax6;
@FXML
private TextField tax7;
@FXML
private TextField tax8;
@FXML
private TextField tax9;
@FXML
private TextField tax10;
@FXML
private TextField tax11;
@FXML
private TextField tax12;
//fxml freight textfields
@FXML
private TextField freight1;
@FXML
private TextField freight2;
@FXML
private TextField freight3;
@FXML
private TextField freight4;
@FXML
private TextField freight5;
@FXML
private TextField freight6;
@FXML
private TextField freight7;
@FXML
private TextField freight8;
@FXML
private TextField freight9;
@FXML
private TextField freight10;
@FXML
private TextField freight11;
@FXML
private TextField freight12;
//fxml labor textfields
@FXML
private TextField labor1;
@FXML
private TextField labor2;
@FXML
private TextField labor3;
@FXML
private TextField labor4;
@FXML
private TextField labor5;
@FXML
private TextField labor6;
@FXML
private TextField labor7;
@FXML
private TextField labor8;
@FXML
private TextField labor9;
@FXML
private TextField labor10;
@FXML
private TextField labor11;
@FXML
private TextField labor12;
//fxml cost textfields
@FXML
private TextField cost1;
@FXML
private TextField cost2;
@FXML
private TextField cost3;
@FXML
private TextField cost4;
@FXML
private TextField cost5;
@FXML
private TextField cost6;
@FXML
private TextField cost7;
@FXML
private TextField cost8;
@FXML
private TextField cost9;
@FXML
private TextField cost10;
@FXML
private TextField cost11;
@FXML
private TextField cost12;
//fxml profit textfields
@FXML
private TextField profit1;
@FXML
private TextField profit2;
@FXML
private TextField profit3;
@FXML
private TextField profit4;
@FXML
private TextField profit5;
@FXML
private TextField profit6;
@FXML
private TextField profit7;
@FXML
private TextField profit8;
@FXML
private TextField profit9;
@FXML
private TextField profit10;
@FXML
private TextField profit11;
@FXML
private TextField profit12;
//fxml button calculate
@FXML
private Button calculateTotal;
@Override
public void initialize(URL url, ResourceBundle rb) {
//initialize combobox variables
comboBox1.setValue("County");
comboBox1.setItems(comboBoxList);
comboBox2.setValue("County");
comboBox2.setItems(comboBoxList);
comboBox3.setValue("County");
comboBox3.setItems(comboBoxList);
comboBox4.setValue("County");
comboBox4.setItems(comboBoxList);
comboBox5.setValue("County");
comboBox5.setItems(comboBoxList);
comboBox6.setValue("County");
comboBox6.setItems(comboBoxList);
comboBox7.setValue("County");
comboBox7.setItems(comboBoxList);
comboBox8.setValue("County");
comboBox8.setItems(comboBoxList);
comboBox9.setValue("County");
comboBox9.setItems(comboBoxList);
comboBox10.setValue("County");
comboBox10.setItems(comboBoxList);
comboBox11.setValue("County");
comboBox11.setItems(comboBoxList);
comboBox12.setValue("County");
comboBox12.setItems(comboBoxList);
}
public void calculate(ActionEvent event){
//add values put into textfields
calculateTotal.setOnAction((EventHandler<ActionEvent>) (event));{
//row1
int sal1 = Integer.parseInt(sale1.getText());
int taxx1 = Integer.parseInt(tax1.getText());
int frei1 = Integer.parseInt(freight1.getText());
int lab1 = Integer.parseInt(labor1.getText());
int cos1 = Integer.parseInt(cost1.getText());
int result1 = sal1 + taxx1 + frei1 + lab1 + cos1;
profit1.setText(profit1.getText()+result1);
//row2
int sal2 = Integer.parseInt(sale2.getText());
int taxx2 = Integer.parseInt(tax2.getText());
int frei2 = Integer.parseInt(freight2.getText());
int lab2 = Integer.parseInt(labor2.getText());
int cos2 = Integer.parseInt(cost2.getText());
int result2 = sal2 + taxx2 + frei2 + lab2 + cos2;
profit2.setText(profit2.getText()+result2);
//row3
int sal3 = Integer.parseInt(sale3.getText());
int taxx3 = Integer.parseInt(tax3.getText());
int frei3 = Integer.parseInt(freight3.getText());
int lab3 = Integer.parseInt(labor3.getText());
int cos3 = Integer.parseInt(cost3.getText());
int result3 = sal3 + taxx3 + frei3 + lab3 + cos3;
profit3.setText(profit3.getText()+result3);
//row4
int sal4 = Integer.parseInt(sale4.getText());
int taxx4 = Integer.parseInt(tax4.getText());
int frei4 = Integer.parseInt(freight4.getText());
int lab4 = Integer.parseInt(labor4.getText());
int cos4 = Integer.parseInt(cost4.getText());
int result4 = sal4 + taxx4 + frei4 + lab4 + cos4;
profit4.setText(profit4.getText()+result4);
//row5
int sal5 = Integer.parseInt(sale5.getText());
int taxx5 = Integer.parseInt(tax5.getText());
int frei5 = Integer.parseInt(freight5.getText());
int lab5 = Integer.parseInt(labor5.getText());
int cos5 = Integer.parseInt(cost5.getText());
int result5 = sal5 + taxx5 + frei5 + lab5 + cos5;
profit5.setText(profit5.getText()+result5);
//row6
int sal6 = Integer.parseInt(sale6.getText());
int taxx6 = Integer.parseInt(tax6.getText());
int frei6 = Integer.parseInt(freight6.getText());
int lab6 = Integer.parseInt(labor6.getText());
int cos6 = Integer.parseInt(cost6.getText());
int result6 = sal6 + taxx6 + frei6 + lab6 + cos6;
profit6.setText(profit6.getText()+result6);
//row7
int sal7 = Integer.parseInt(sale7.getText());
int taxx7 = Integer.parseInt(tax7.getText());
int frei7 = Integer.parseInt(freight7.getText());
int lab7 = Integer.parseInt(labor7.getText());
int cos7 = Integer.parseInt(cost7.getText());
int result7 = sal7 + taxx7 + frei7 + lab7 + cos7;
profit7.setText(profit7.getText()+result7);
//row8
int sal8 = Integer.parseInt(sale8.getText());
int taxx8 = Integer.parseInt(tax8.getText());
int frei8 = Integer.parseInt(freight8.getText());
int lab8 = Integer.parseInt(labor8.getText());
int cos8 = Integer.parseInt(cost8.getText());
int result8 = sal8 + taxx8 + frei8 + lab8 + cos8;
profit8.setText(profit8.getText()+result8);
//row9
int sal9 = Integer.parseInt(sale9.getText());
int taxx9 = Integer.parseInt(tax9.getText());
int frei9 = Integer.parseInt(freight9.getText());
int lab9 = Integer.parseInt(labor9.getText());
int cos9 = Integer.parseInt(cost9.getText());
int result9 = sal9 + taxx9 + frei9 + lab9 + cos9;
profit9.setText(profit9.getText()+result9);
//row10
int sal10 = Integer.parseInt(sale10.getText());
int taxx10 = Integer.parseInt(tax10.getText());
int frei10 = Integer.parseInt(freight10.getText());
int lab10 = Integer.parseInt(labor10.getText());
int cos10 = Integer.parseInt(cost10.getText());
int result10 = sal10 + taxx10 + frei10 + lab10 + cos10;
profit10.setText(profit10.getText()+result10);
//row11
int sal11 = Integer.parseInt(sale11.getText());
int taxx11 = Integer.parseInt(tax11.getText());
int frei11 = Integer.parseInt(freight11.getText());
int lab11 = Integer.parseInt(labor11.getText());
int cos11 = Integer.parseInt(cost11.getText());
int result11 = sal11 + taxx11 + frei11 + lab11 + cos11;
profit11.setText(profit11.getText()+result11);
//row12
int sal12 = Integer.parseInt(sale12.getText());
int taxx12 = Integer.parseInt(tax12.getText());
int frei12 = Integer.parseInt(freight12.getText());
int lab12 = Integer.parseInt(labor12.getText());
int cos12 = Integer.parseInt(cost12.getText());
int result12 = sal12 + taxx12 + frei12 + lab12 + cos12;
profit12.setText(profit12.getText()+result12);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package taxerfxml;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.swing.JComboBox;
import static sun.misc.Signal.handle;
這裏是我的更新文件。林現在calculateTotal得到一個NullPointerException從第一行()
/**
*
* @author
*/
public class FXMLDocumentController implements Initializable {
ObservableList<String> comboBoxList = FXCollections.observableArrayList("Ccosta", "Alam", "Sclara", "Smateo", "Sf", "Marin", "Sonoma", "Napa", "Solono");
//fxml combo boxes
@FXML
private ComboBox comboBox1;
@FXML
private ComboBox comboBox2;
@FXML
private ComboBox comboBox3;
@FXML
private ComboBox comboBox4;
@FXML
private ComboBox comboBox5;
@FXML
private ComboBox comboBox6;
@FXML
private ComboBox comboBox7;
@FXML
private ComboBox comboBox8;
@FXML
private ComboBox comboBox9;
@FXML
private ComboBox comboBox10;
@FXML
private ComboBox comboBox11;
@FXML
private ComboBox comboBox12;
//fxml labels
@FXML
private Label sale;
@FXML
private Label tax;
@FXML
private Label county;
@FXML
private Label freight;
@FXML
private Label labor;
@FXML
private Label cost;
@FXML
private Label profit;
@FXML
private Label customer;
// fxml customer textfields
@FXML
private TextField customer1;
@FXML
private TextField customer2;
@FXML
private TextField customer3;
@FXML
private TextField customer4;
@FXML
private TextField customer5;
@FXML
private TextField customer6;
@FXML
private TextField customer7;
@FXML
private TextField customer8;
@FXML
private TextField customer9;
@FXML
private TextField customer10;
@FXML
private TextField customer11;
@FXML
private TextField customer12;
//fxml sale textfields
@FXML
private TextField sale1;
@FXML
private TextField sale2;
@FXML
private TextField sale3;
@FXML
private TextField sale4;
@FXML
private TextField sale5;
@FXML
private TextField sale6;
@FXML
private TextField sale7;
@FXML
private TextField sale8;
@FXML
private TextField sale9;
@FXML
private TextField sale10;
@FXML
private TextField sale11;
@FXML
private TextField sale12;
//fxml tax textfields
@FXML
private TextField tax1;
@FXML
private TextField tax2;
@FXML
private TextField tax3;
@FXML
private TextField tax4;
@FXML
private TextField tax5;
@FXML
private TextField tax6;
@FXML
private TextField tax7;
@FXML
private TextField tax8;
@FXML
private TextField tax9;
@FXML
private TextField tax10;
@FXML
private TextField tax11;
@FXML
private TextField tax12;
//fxml freight textfields
@FXML
private TextField freight1;
@FXML
private TextField freight2;
@FXML
private TextField freight3;
@FXML
private TextField freight4;
@FXML
private TextField freight5;
@FXML
private TextField freight6;
@FXML
private TextField freight7;
@FXML
private TextField freight8;
@FXML
private TextField freight9;
@FXML
private TextField freight10;
@FXML
private TextField freight11;
@FXML
private TextField freight12;
//fxml labor textfields
@FXML
private TextField labor1;
@FXML
private TextField labor2;
@FXML
private TextField labor3;
@FXML
private TextField labor4;
@FXML
private TextField labor5;
@FXML
private TextField labor6;
@FXML
private TextField labor7;
@FXML
private TextField labor8;
@FXML
private TextField labor9;
@FXML
private TextField labor10;
@FXML
private TextField labor11;
@FXML
private TextField labor12;
//fxml cost textfields
@FXML
private TextField cost1;
@FXML
private TextField cost2;
@FXML
private TextField cost3;
@FXML
private TextField cost4;
@FXML
private TextField cost5;
@FXML
private TextField cost6;
@FXML
private TextField cost7;
@FXML
private TextField cost8;
@FXML
private TextField cost9;
@FXML
private TextField cost10;
@FXML
private TextField cost11;
@FXML
private TextField cost12;
//fxml profit textfields
@FXML
private TextField profit1;
@FXML
private TextField profit2;
@FXML
private TextField profit3;
@FXML
private TextField profit4;
@FXML
private TextField profit5;
@FXML
private TextField profit6;
@FXML
private TextField profit7;
@FXML
private TextField profit8;
@FXML
private TextField profit9;
@FXML
private TextField profit10;
@FXML
private TextField profit11;
@FXML
private TextField profit12;
//fxml button calculate
@FXML
private Button calculate;
@Override
public void initialize(URL url, ResourceBundle rb) {
//initialize combobox variables
comboBox1.setValue("County");
comboBox1.setItems(comboBoxList);
comboBox2.setValue("County");
comboBox2.setItems(comboBoxList);
comboBox3.setValue("County");
comboBox3.setItems(comboBoxList);
comboBox4.setValue("County");
comboBox4.setItems(comboBoxList);
comboBox5.setValue("County");
comboBox5.setItems(comboBoxList);
comboBox6.setValue("County");
comboBox6.setItems(comboBoxList);
comboBox7.setValue("County");
comboBox7.setItems(comboBoxList);
comboBox8.setValue("County");
comboBox8.setItems(comboBoxList);
comboBox9.setValue("County");
comboBox9.setItems(comboBoxList);
comboBox10.setValue("County");
comboBox10.setItems(comboBoxList);
comboBox11.setValue("County");
comboBox11.setItems(comboBoxList);
comboBox12.setValue("County");
comboBox12.setItems(comboBoxList);
}
@FXML
public void calculateTotal(ActionEvent event){
//add values put into textfields
// calculateTotal.setOnAction(new EventHandler<ActionEvent>() {
// if(event.getSource()==calculateTotal){
//row1
int sal1 = Integer.parseInt(sale1.getText());
int taxx1 = Integer.parseInt(tax1.getText());
int frei1 = Integer.parseInt(freight1.getText());
int lab1 = Integer.parseInt(labor1.getText());
int cos1 = Integer.parseInt(cost1.getText());
int result1 = sal1 + taxx1 + frei1 + lab1 + cos1;
profit1.setText(profit1.getText()+result1);
//row2
int sal2 = Integer.parseInt(sale2.getText());
int taxx2 = Integer.parseInt(tax2.getText());
int frei2 = Integer.parseInt(freight2.getText());
int lab2 = Integer.parseInt(labor2.getText());
int cos2 = Integer.parseInt(cost2.getText());
int result2 = sal2 + taxx2 + frei2 + lab2 + cos2;
profit2.setText(profit2.getText()+result2);
//row3
int sal3 = Integer.parseInt(sale3.getText());
int taxx3 = Integer.parseInt(tax3.getText());
int frei3 = Integer.parseInt(freight3.getText());
int lab3 = Integer.parseInt(labor3.getText());
int cos3 = Integer.parseInt(cost3.getText());
int result3 = sal3 + taxx3 + frei3 + lab3 + cos3;
profit3.setText(profit3.getText()+result3);
//row4
int sal4 = Integer.parseInt(sale4.getText());
int taxx4 = Integer.parseInt(tax4.getText());
int frei4 = Integer.parseInt(freight4.getText());
int lab4 = Integer.parseInt(labor4.getText());
int cos4 = Integer.parseInt(cost4.getText());
int result4 = sal4 + taxx4 + frei4 + lab4 + cos4;
profit4.setText(profit4.getText()+result4);
//row5
int sal5 = Integer.parseInt(sale5.getText());
int taxx5 = Integer.parseInt(tax5.getText());
int frei5 = Integer.parseInt(freight5.getText());
int lab5 = Integer.parseInt(labor5.getText());
int cos5 = Integer.parseInt(cost5.getText());
int result5 = sal5 + taxx5 + frei5 + lab5 + cos5;
profit5.setText(profit5.getText()+result5);
//row6
int sal6 = Integer.parseInt(sale6.getText());
int taxx6 = Integer.parseInt(tax6.getText());
int frei6 = Integer.parseInt(freight6.getText());
int lab6 = Integer.parseInt(labor6.getText());
int cos6 = Integer.parseInt(cost6.getText());
int result6 = sal6 + taxx6 + frei6 + lab6 + cos6;
profit6.setText(profit6.getText()+result6);
//row7
int sal7 = Integer.parseInt(sale7.getText());
int taxx7 = Integer.parseInt(tax7.getText());
int frei7 = Integer.parseInt(freight7.getText());
int lab7 = Integer.parseInt(labor7.getText());
int cos7 = Integer.parseInt(cost7.getText());
int result7 = sal7 + taxx7 + frei7 + lab7 + cos7;
profit7.setText(profit7.getText()+result7);
//row8
int sal8 = Integer.parseInt(sale8.getText());
int taxx8 = Integer.parseInt(tax8.getText());
int frei8 = Integer.parseInt(freight8.getText());
int lab8 = Integer.parseInt(labor8.getText());
int cos8 = Integer.parseInt(cost8.getText());
int result8 = sal8 + taxx8 + frei8 + lab8 + cos8;
profit8.setText(profit8.getText()+result8);
//row9
int sal9 = Integer.parseInt(sale9.getText());
int taxx9 = Integer.parseInt(tax9.getText());
int frei9 = Integer.parseInt(freight9.getText());
int lab9 = Integer.parseInt(labor9.getText());
int cos9 = Integer.parseInt(cost9.getText());
int result9 = sal9 + taxx9 + frei9 + lab9 + cos9;
profit9.setText(profit9.getText()+result9);
//row10
int sal10 = Integer.parseInt(sale10.getText());
int taxx10 = Integer.parseInt(tax10.getText());
int frei10 = Integer.parseInt(freight10.getText());
int lab10 = Integer.parseInt(labor10.getText());
int cos10 = Integer.parseInt(cost10.getText());
int result10 = sal10 + taxx10 + frei10 + lab10 + cos10;
profit10.setText(profit10.getText()+result10);
//row11
int sal11 = Integer.parseInt(sale11.getText());
int taxx11 = Integer.parseInt(tax11.getText());
int frei11 = Integer.parseInt(freight11.getText());
int lab11 = Integer.parseInt(labor11.getText());
int cos11 = Integer.parseInt(cost11.getText());
int result11 = sal11 + taxx11 + frei11 + lab11 + cos11;
profit11.setText(profit11.getText()+result11);
//row12
int sal12 = Integer.parseInt(sale12.getText());
int taxx12 = Integer.parseInt(tax12.getText());
int frei12 = Integer.parseInt(freight12.getText());
int lab12 = Integer.parseInt(labor12.getText());
int cos12 = Integer.parseInt(cost12.getText());
int result12 = sal12 + taxx12 + frei12 + lab12 + cos12;
profit12.setText(profit12.getText()+result12);
//});
}
}
這裏是我shouldve包括FXML文件。我把它縮小到了真正只顯示按鈕聲明的地方,因爲問題來自哪裏。
<Button fx:id="calculate" layoutX="976.0" layoutY="23.0" mnemonicParsing="false" onAction="#calculateTotal" text="Calculate" />
要了解按鈕實際上是射擊,你應該簡單地把一個的System.out.println(」你只是按下按鈕「);在頂部的calculateTotal處理程序中。如果您在按下按鈕後看到此代碼,則知道該按鈕正在觸發,並且您的問題存在於其他位置。要嘗試查看代碼是否拋出了未捕獲的錯誤,請將所有代碼放在try塊中,然後在catch框中使用Exception ex並打印ex.toString(); – Sedrick