在GUI中逐项显示数组列表

时间:2013-11-27 03:54:42

标签: java user-interface arraylist append

我正在尝试逐项显示包含多种类型的数组列表。所以我想这是一个两部分的问题。首先,如何将此数组列表转换为字符串以将其打印出来(我不确定这是多么重要)。第二,如何使用ActionEvent从一个项目移动到另一个项目。

My Array列表包含String,int,int,double。我已经想出如何将double格式化为字符串,但是我如何为整个arraylist执行此操作? arraylist已经排序,以便按字母顺序显示。

下面是我的数组列表和排序

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

}

我的按钮设置如下

 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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

应在Object#toString()中覆盖

InventoryItem以获得所需的显示结果。例如:

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时,它看起来像这样

  

输出: Type: pencil, Id: 1111, Qty: 50, Cost: .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");
        }
    }
});

注意:考虑Java命名约定。班级名称应以大写字母开头

答案 1 :(得分:0)

  1. 要显示对象的属性,您需要实现inventoryItem对象的'toString()'方法(请注意JAVA类名称应以大写字母开头)
  2. 在toString方法中,您可以创建一个字符串,因为您甚至可以连接属性名称。例如,您可以像这样显示它 商品名称:笔,商品编号:1111 ..
  3. 由于你需要向前和向后遍历,你应该考虑使用linkedlist或doublelylinkedlist集合。