我正在尝试创建一个注册系统。我有6个复选框,我想要发生的是如果我选中其中一个复选框并按下按钮计算,它应该给标签值2640.当我点击另一个复选框并按下按钮再次计算值如果我点击所有6个复选框,标签是5280,依此类推。
如果没有点击一个复选框并按下计算按钮,它应该从当前总数中减去2640。这是我的代码:
if (chkPLF.isSelected() == true) {
tFee = 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee + tFee));
if (chkPLF.isSelected() == false) {
tFee = tFee - 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
}
}
if (chkSAD.isSelected() == true) {
tFee = 2640 * 3;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
if (chkSAD.isSelected() == false) {
tFee = tFee - 2640;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
}
}
它只显示当前值但不减去。请帮助!
答案 0 :(得分:1)
double amount = 0;
int sel = 0;
if(jCheckBox1.isSelected()) sel++;
if(jCheckBox2.isSelected()) sel++;
if(jCheckBox3.isSelected()) sel++;
if(jCheckBox4.isSelected()) sel++;
if(jCheckBox5.isSelected()) sel++;
if(jCheckBox6.isSelected()) sel++;
amount = 2640 * sel;
JOptionPane.showMessageDialog(rootPane, String.valueOf(amount));
将此代码放入“计算”中点击ActionListener
答案 1 :(得分:1)
为什么不计算点击结果的复数的数量是2640的结果。现在,你似乎用每个复选框覆盖了结果......
例如......
int checkCount = 0;
if (chk1.isSelected()) {
checkCount++;
}
if (chk2.isSelected()) {
checkCount++;
}
if (chk3.isSelected()) {
checkCount++;
}
if (chk4.isSelected()) {
checkCount++;
}
if (chk5.isSelected()) {
checkCount++;
}
if (chk6.isSelected()) {
checkCount++;
}
tFee = 2640 * checkCount;
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
现在,我要走了,这可以通过循环轻松完成......
JCheckBox[] boxes = new JCheckBox[]{chk1, chk2, chk3, chk4, chk5, chk6};
tFee = 0;
for (JCheckBox box : boxes) {
if (box.isSelected()) {
tFee += 2640;
}
}
lblMis.setText(Double.toString(misFee));
lblT.setText(Double.toString(tFee));
但也许那是因为我疯了......