【发布时间】:2016-10-28 21:00:16
【问题描述】:
我需要检查 JList / DefaultListModel 是否包含项目。我正在检查的项目是一个在“$”符号后发生变化的字符串。
这是我正在使用的代码的伪版本。
String theItem = "Bananas";
BigDecimal theQuantity = new BigDecimal(quantity.getText());
BigDecimal thePrice = new BigDecimal(0.00); //This changes depending on quanitity
thePrice = thePrice.setScale(2, BigDecimal.ROUND_HALF_UP);
if (!dlm.contains(whatGoesHere)) {
dlm.addElement(theItem + " $" + thePrice.toString());
jList.setModel(dlm);
//More code
} else {
JOptionPane.showMessageDialog(mainPanel, "You already selected that item", "Error Dialog", JOptionPane.ERROR_MESSAGE);
return;
}
【问题讨论】:
-
你研究过正则表达式和模式匹配吗?
-
可能有一个侦听器设置为
dml对象,该对象在dlm.addElement()中被调用 -
或者使用一个类(例如
Product或Item)来保存描述和价格,并适当地实现toString和equals。那样的额外学分:) -
@PaulHicks,你愿意给我一个关于如何制作这门课的提示吗?
-
随意删除问题或发布您自己的答案(为了他人的利益)
标签: java jlist defaultlistmodel