java - 在 GUI 中逐项显示数组列表

标签 java user-interface arraylist append

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

我的数组列表有 String、int、int、double。我已经弄清楚如何将 double 型格式化为字符串,但是如何对整个数组列表执行此操作?数组列表已经排序以便按字母顺序显示。

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

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

任何帮助将不胜感激。

最佳答案

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

Output: 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 命名约定。类名应以大写字母开头

关于java - 在 GUI 中逐项显示数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233565/

相关文章:

java - 去掉 toString 中的某些逗号等

java - runOnUi 减慢应用程序速度并使其强制关闭

Java:从新的 JFrame 获取复选框名称

java - 尝试使用maven程序集插件时出现 "Assembly is incorrectly configured: null"

java - 两个 Eclipse UI Form 部分捕获多余的垂直空间并正确折叠?

ios - 如何在iOS上实现 "vertical reveal"的效果,最好是用UITableView

java - 将我的 double 单元格变成 double 数组

java - 并发 hashmap 可以处理多达 10000 个线程而不发生死锁吗?

android - 如何创建类似 Foursquare 兴趣选择屏幕的 gridview?

java - List.of 给出编译错误 "The method of(char, char, char, char, char, char) is undefined for the type List"