[英]Java, Add together a value of elements in a JList
我有一个jList包含具有3个参数的元素。 元素是
Item(double code, String name, double price)
我正在尝试获取列表中所有元素的总价并将其显示在文本框中。 这可能吗?如果可以,怎么办?
从JList获取ListModel
ListModel model = instanceOfJList.getModel();
遍历模型元素...
double sum = 0;
for (int index = 0; index < model.size(); index++) {
Item item = (Item)model.getElementAt(index);
sum += item.getPrice();
}
// Display sum somewhere on the screen...
好吧,您可能必须遍历列表,并在每个元素上调用类似getPrice()的方法来创建总和。
您的商品类别是否有价格的吸气剂?
如果是,则可以遍历列表,每次获取价格并将其添加到变量中; 称之为totalSum。
最后,您应该能够在文本框中显示totalSum。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.