Java 在GUI中逐项显示数组列表
我试图逐项显示具有多种类型的数组列表。所以我想这是一个由两部分组成的问题。首先,如何将此数组列表转换为字符串以将其打印出来(我不确定这有多重要)。第二,如何使用ActionEvent从一个项目移动到另一个项目 我的数组列表有String,int,int,double。我已经知道如何将double格式化为字符串,但是如何对整个arraylist执行此操作?arraylist已排序,以便按字母顺序显示 下面是我的数组列表和排序Java 在GUI中逐项显示数组列表,java,user-interface,arraylist,append,Java,User Interface,Arraylist,Append,我试图逐项显示具有多种类型的数组列表。所以我想这是一个由两部分组成的问题。首先,如何将此数组列表转换为字符串以将其打印出来(我不确定这有多重要)。第二,如何使用ActionEvent从一个项目移动到另一个项目 我的数组列表有String,int,int,double。我已经知道如何将double格式化为字符串,但是如何对整个arraylist执行此操作?arraylist已排序,以便按字母顺序显示 下面是我的数组列表和排序 inventory.add(new inventoryItem("Pen
inventory.add(new inventoryItem("Pencil", 1111, 50, .25));
inventory.add(new inventoryItem("Pen", 2222, 50, 1.00));
inventory.add(new inventoryItem("Marker", 3333, 5, 2.00));
inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
inventory.add(new officeSupplyItem("Mechanical Pencil", 1112, 25, .50));
inventory.add(new officeSupplyItem("Lead Pencil", 1113, 25, .25));
inventory.add(new officeSupplyItem("Blue Pen", 2221, 25, 1.00));
inventory.add(new officeSupplyItem("Black Pen", 2223, 5, 1.00));
inventory.add(new officeSupplyItem("Red Pen", 2224, 20, 1.00));
inventory.add(new officeSupplyItem("Steno Notebook", 4441, 5, 2.50));
inventory.add(new officeSupplyItem("Legal Pad", 4442, 5, 2.50));
inventory = sortInventory(inventory);
for (int i = 0; i < inventory.size(); i++) {
inventory.get(i).output(outputText);
}
inventoryTotal(inventory, outputText);
sortInventory(inventory);
}
static ArrayList sortInventory(ArrayList<inventoryItem> unsorted) {
ArrayList<inventoryItem> sorted = new ArrayList<>(); //create new array list to sort
inventoryItem alpha = null;
int lowestIndex = -1;
while (unsorted.size() > 0) { //while my unsorted array is less than 0 do the following
for (int i = 0; i < unsorted.size(); i++) { //increment through
if (alpha == null) {
alpha = unsorted.get(i); //get the next line in the inventoryItem array
lowestIndex = i;
} else if (unsorted.get(i).itemName.compareToIgnoreCase(alpha.itemName) < 0) { //compare items to determine which has a higher value
alpha = unsorted.get(i);
lowestIndex = i;
}
}
sorted.add(alpha); //reset the index so it will loop until there are no more items in the unsorted array
unsorted.remove(lowestIndex);
alpha = null;
lowestIndex = -1;
}
return sorted; //return the sorted arraylist
}
任何帮助都将不胜感激
InventoryItem
中应覆盖Object#toString()
,以获得所需的显示结果。例如:
public class InventoryItem {
String type;
int id;
int quantity;
double cost;
public InventoryItem(String type, int id, int quantity, double cost){
this.type = type;
this.id = id;
this.quantity = quantity;
this.cost = cost;
}
// override toString
@Override
public void toString(){
return "Type: " + type
+ ", Id: " + id
+ ", Qty: " + quantity
+ ", Cost: " + cost;
}
}
当您打印一个InventoryItem
时,它的外观如下所示
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputText.append("\n this should move me forward in the list");
}
});
previousButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
outputText.append("\n this should move me backward in the list");
}
});
exitButton.addActionListener(new ActionListener() { //setup listener for the exit button
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0); //once the exit button is pressed, exit the program
}
});
thePanel.add(nextButton); //move forward in the arraylist
thePanel.add(previousButton); //move to previous item in the arraylist
thePanel.add(exitButton); //add exit button to the panel
输出:类型:铅笔,Id:1111,数量:50,成本:.25
要从一个索引移动到另一个索引,可以保留一个currentIndex
变量
int currentIndex = 0;
ArrayList<InventoryItem> inventory = new ArraList<>();
previousButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if (currentIndex > 0) {
currentIndex--;
outputText.append(inventory.get(currentIndex) + "\n");
}
}
});
int currentIndex=0;
ArrayList inventory=新的ArraList();
previousButton.addActionListener(新ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
如果(当前索引>0){
当前索引--;
outputText.append(inventory.get(currentIndex)+“\n”);
}
}
});
注意:考虑java命名约定。类名应该以大写字母开头你的问题是什么?它实际上是两部分。我想我必须使用一个列表迭代器,才能在arraylist中逐项向前和向后移动,这样做应该是字符串正确的吗?所以我的第一个问题是如何将我的arraylist转换为每行一个字符串?第二个问题是如何结合使用列表迭代器和gui按钮在列表中移动?“将我的arraylist转换为一个字符串”对它进行迭代,并在每个元素上调用
toString()
。这似乎工作得很好,只是不能使currentIndex工作。我要么从静态上下文调用它有问题,要么它找不到变量。它不应该放在主类中吗?将它放在您声明的相同位置ArrayList inventory=new ArraList()代码>。我不知道你的代码到底是什么样子的,但是如果第一个不起作用,试着让currentIndex
static。虽然很难说你的代码是结构化的。如果您试图从static
方法调用currentIndex
,那么它也应该是静态的。currentIndex
应该在一个全局范围内,如果这有帮助的话。我已经在我的公共类测试中声明了它,它在我的main之前-如果这有帮助的话。我宣布它是私人的,但我不确定这是否正确。什么不起作用?代码中可能还有其他问题。首先,我在您的sortInventory()
方法中看到,您正在创建一个新数组列表,而不是修改原始数组列表。然后您尝试以静态方式调用它(不给它分配变量并使用该变量)sortInventory(inventory)代码>。可能有多方面的问题。这可能不是你根据我的建议所做的修改。