【发布时间】:2013-12-04 03:48:41
【问题描述】:
我目前有一个工作的 GUI 程序,上面有几个按钮可以简单地逐步浏览我的项目数组列表。这些只是显示第一个、最后一个或下一个和上一个索引结果。该列表包含String、int 和double。现在我添加了一个JTextField 用于输入和一个搜索按钮。我的问题是如何让我的搜索按钮来搜索这个数组列表?我正在阅读this answer,但我不明白基准的事情。在搜索之前是否必须将整个数组列表转换为字符串?像
ArrayList<inventoryItem> inventory = new ArrayList<>(); ....
JTextField input = new JTextField(18); ...
JButton searchButton = new JButton("Search");
searchButton.setToolTipText("Search for entry");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String usrInput = input.getText();
for (String s : inventory) {
if (usrInput.contains(s)) {
inventory.get(currentIndex);
outputText.append(" somehow put whatever the index is equal to here");
}
}
}
});
我得到的错误是inventoryItem cannot be converted to string。第二个问题:我遇到的是如何让它输出该索引中的所有内容。例如,我的输出如下所示:
class officeSupplyItem extends inventoryItem {
public officeSupplyItem(String itemName, int itemNumber, int inStock, double unitPrice) {
super(itemName, itemNumber, inStock, unitPrice);
}
@Override
public void output(JTextArea outputText) {
outputText.setText("Item Name = " + itemName + " \n"); //print out the item name
outputText.append("Item Number = " + itemNumber + " \n"); //print out the item number
outputText.append("In Stock = " + inStock + " \n"); //print out how many of the item are in stock
outputText.append("Item Price = $" + formatted.format(unitPrice) + " \n"); //print out the price per item
outputText.append("Restocking fee is $" + formatted.format(restockingFee) + " per item \n");
outputText.append("Value of item inventory = $" + formatted.format(value) + " \n"); //print out the value of the item inventory
outputText.append("Cost of inventory w/restocking fee = $" + formatted.format(inventoryValue) + " \n"); //print out the total cost of inventory with restocking fee
}
}
我还想了解上述链接的基准部分的含义。
【问题讨论】:
标签: java search user-interface netbeans arraylist