好的,我有一个GUI,它接受文本输入(比如说10),然后你可以勾选你要添加到初始量的内容(四个选项10,20,50,100)。之后,我试图在另一个文本框架中显示项目的总和。
int firstMuffler = Integer.parseInt(inputMufflers.getText());
int firstTire = Integer.parseInt(inputTires.getText());
int firstBreaks = Integer.parseInt(inputBreaks.getText());
int firstMirrors = Integer.parseInt(inputMirrors.getText());
if (Muf10.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 10));
}
else if (Muf20.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 20));
}
else if (Muf50.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 50));
}
else if (Muf100.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 100));
}
else if (T10.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 10));
}
else if (T20.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 20));
}
else if (T50.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 50));
}
else if (T100.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 100));
}
else if (B10.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 10));
}
else if (B20.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 20));
}
else if (B50.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 50));
}
else if (B100.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 100));
}
else if (M10.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 10));
}
else if (M20.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors+ 20));
}
else if (M50.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 50));
}
else if (M100.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 100));
}else{
}
这就是我所拥有的,我无法弄清楚是什么让它无法运作。因为它只输出FinalStockMuf
文本框,其余的不起作用。我还必须填写所有内容以使其正常工作,如零。
注意:Muf10
,T10
等。具有这些值的复选框。
firstTire
,firstMug
等。是输入的第一个价格,未选中任何复选框。
答案 0 :(得分:0)
当您使用else if
梯形图时,您需要记住只执行一个符合要求的选项。在您的示例中,您实现了负责从梯形图中的其他一个对象中收集和插入数据的代码。实际上,满足条件工作的第一个选项和休息被忽略。因此,您应该像这样更改代码:
if (Muf10.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 10));
}
else if (Muf20.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 20));
}
else if (Muf50.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 50));
}
else if (Muf100.isSelected()){
FinalStockMuf.setText(String.valueOf(firstMuffler + 100));
}
//break and beginning of new else if structure,
if (T10.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 10));
}
else if (T20.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 20));
}
else if (T50.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 50));
}
else if (T100.isSelected()){
FinalStockT.setText(String.valueOf(firstTire + 100));
}
//break and beginning of new else if structure,
if (B10.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 10));
}
else if (B20.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 20));
}
else if (B50.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 50));
}
else if (B100.isSelected()){
FinalStockB.setText(String.valueOf(firstBreaks + 100));
}
//break and beginning of new else if structure,
if (M10.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 10));
}
else if (M20.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors+ 20));
}
else if (M50.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 50));
}
else if (M100.isSelected()){
FinalStockM.setText(String.valueOf(firstMirrors + 100));
}
它应该工作得很好。
下一个问题是NumberFormatException: For input string: ""
,当任何输入文本字段为空时抛出。要解决这个问题,我会使用:
int firstMuffler = (!inputMufflers.getText().equals("")) ? Integer.parseInt(inputMufflers.getText()) : 0;
...以及每个下一个JTextField
的类似解决方案。此代码检查JTextField
是否为空,如果为空,则为firstMuffler
提供0值。但是,它并没有使用' 0'填充JTextField
。
要使用未修改的库存值输入输出JTextField
,或使用' 0'输入和输出JTextField。如果没有输入,只需添加例如:
}else if(inputMufflers.getText().equals("")){
inputMufflers.setText("0");
FinalStockMuf.setText("0");
}else{
FinalStockMuf.setText(inputMufflers.getText());
}
到每个else if
部分的结尾。
我希望你会发现我的答案很有用。