【发布时间】:2017-07-06 19:44:48
【问题描述】:
我当前的代码只会附加最后一个选中的复选框。我需要从选中的复选框中获取所有值并附加到 jtextarea 以便稍后打印。也许是一个数组,但不确定如何附加一个数组。感谢您的帮助!
编辑:我解决了这个问题!我没有将值传递给 append 方法,而是将 append 方法传递给值以获得我想要的输出。我发布了更新的代码。
private void jBtnRecieptActionPerformed(java.awt.event.ActionEvent evt) {
//declare local variables
String item = null;
int quanity = 0;
double priceEa = 0;
double total = 0;
if (jCTiteliestGolfBall.isSelected()) {
quanity = Integer.parseInt(jQty1.getText());
item = jCTiteliestGolfBall.getActionCommand();
priceEa = 1.50;
total = (priceEa * quanity);
}
if (jCTiteliestGolfClubs.isSelected()) {
quanity = Integer.parseInt(jQty2.getText());
item = jCTiteliestGolfClubs.getActionCommand();
priceEa = 150.00;
total = (priceEa * quanity);
}
if (jCGregNormanGolfShirt.isSelected()) {
quanity = Integer.parseInt(jQty3.getText());
item = jCGregNormanGolfShirt.getActionCommand();
priceEa = 15.00;
total = (priceEa * quanity);
}
if (jCGregNormanGolfHat.isSelected()) {
quanity = Integer.parseInt(jQty4.getText());
item = jCGregNormanGolfHat.getActionCommand();
priceEa = 10.00;
total = (priceEa * quanity);
}
if (jCGolfGlove.isSelected()) {
quanity = Integer.parseInt(jQty5.getText());
item = jCGolfGlove.getActionCommand();
priceEa = 2.00;
total = (priceEa * quanity);
}
if (jCGolfTees.isSelected()) {
quanity = Integer.parseInt(jQty6.getText());
item = jCGolfTees.getActionCommand();
priceEa = .50;
total = (priceEa * quanity);
}
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
date = (dtf.format(now));
jTextAreaReciept.append("\tFolly Beach Golf:\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)
+ "\nSubtotal: " + jTextFieldCostofItems.getText()
+ "\nTax: " + jTextFieldTax.getText()
+ "\nTotal: " + jTextFieldTotal.getText()
+ "\n\nDate/Time: " + date + "\n\n\tThank you for your business");
更新:
私人无效 jBtnRecieptActionPerformed(java.awt.event.ActionEvent evt) {
//声明局部变量
字符串项="";
整数 = 0;
双倍价格Ea = 0;
双倍总计 = 0;
jTextAreaReciept.append("\tFolly Beach Golf:\n");
if (jCTiteliestGolfBall.isSelected()) {
quanity = Integer.parseInt(jQty1.getText());
item = jCTiteliestGolfBall.getActionCommand();
priceEa = 1.50;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
if (jCTiteliestGolfClubs.isSelected()) {
quanity = Integer.parseInt(jQty2.getText());
item = jCTiteliestGolfClubs.getActionCommand();
priceEa = 150.00;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
if (jCGregNormanGolfShirt.isSelected()) {
quanity = Integer.parseInt(jQty3.getText());
item = jCGregNormanGolfShirt.getActionCommand();
priceEa = 15.00;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
if (jCGregNormanGolfHat.isSelected()) {
quanity = Integer.parseInt(jQty4.getText());
item = jCGregNormanGolfHat.getActionCommand();
priceEa = 10.00;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
if (jCGolfGlove.isSelected()) {
quanity = Integer.parseInt(jQty5.getText());
item = jCGolfGlove.getActionCommand();
priceEa = 2.00;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
if (jCGolfTees.isSelected()) {
quanity = Integer.parseInt(jQty6.getText());
item = jCGolfTees.getActionCommand();
priceEa = .50;
total = (priceEa * quanity);
jTextAreaReciept.append("\n" + item + " " + quanity + " @ "
+ String.format("%.2f", priceEa) + " = " + String.format("%.2f", total));
}
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
date = (dtf.format(now));
jTextAreaReciept.append("\n\nSubtotal: " + jTextFieldCostofItems.getText()
+ "\nTax: " + jTextFieldTax.getText()
+ "\nTotal: " + jTextFieldTotal.getText()
+ "\n\nDate/Time: " + date + "\n\n\tThank you for your business");
【问题讨论】: