我希望从JTextField
获取所有数据并将它们一起添加,然后将答案输出到文本文件中。
static class Session implements ActionListener {
public void actionPerformed (ActionEvent e){
JButton submitInvoice = new JButton ("Submit");
sPanel.add(submitInvoice);
submitInvoice.addActionListener(e8->{
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("RegInvoice.txt"));
bw.write("---------------Booking Invoice---------------");
bw.write("\r\n");
bw.write("All Day: "); bw.write(tSesh1.getText());
bw.write("\r\n");
bw.write("Morning: "); bw.write(tSesh2.getText());
bw.write("\r\n");
bw.write("Lunch: "); bw.write(tSesh3.getText());
bw.write("\r\n");
bw.write("Afternoon: "); bw.write(tSesh4.getText());
bw.write("\r\n");
bw.write("Pre School: "); bw.write(tSesh5.getText());
bw.write("\r\n");
bw.write("Full Holiday Care: "); bw.write(tSesh6.getText());
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
});
}
}
上面的代码采用tSesh1,tSesh2,tSesh3,tSesh4,tSesh5并将它们打印到文件中。但我需要将每个JTextField
添加到一起。
答案 0 :(得分:4)
您可以通过解析TEXTFIELD.getText()返回的字符串来获取每个字段的整数值
int field1 = Integer.parseInt(TEXTFIELD1.getText());
int field2 = Integer.parseInt(TEXTFIELD2.getText());
您可以根据需要使用尽可能多的jTextField。然后,您可以使用整数进行必要的计算。
即
int total = field1 + field2;