2016-01-21 69 views
0

下面是創建具有代碼編號名稱,價格和數量的項目的代碼。使用hashmap在JTextField中顯示數據並更新它

public class StockData { 
    private static class Item { 
     Item(String n, double p, int q) { 
      name = n; 
      price = p; 
      quantity = q; 
     } 
     public String getName() { 
      return name; 
     } 
     public double getPrice() { 
      return price; 
     } 
     public int getQuantity() { 
      return quantity; 
     } 
     private final String name; 
     private final double price; 
     private int quantity; 
    } 
    public final static Map<String, Item> stock = new HashMap(); 
    static { 
     stock.put("00", new Item("Bath towel", 5.50, 10)); 
     stock.put("11", new Item("Plebney light", 20.00, 5)); 
     stock.put("22", new Item("Gorilla suit", 30.00, 7)); 
     stock.put("33", new Item("Whizz games console", 50.00, 8)); 
     stock.put("44", new Item("Oven", 200.00, 4)); 
    } 
    public static Map<String, Item> getStock() { 
     return stock; 
    } 
    public static String getName(String key) { 
     Item item = stock.get(key); 
     if (item == null) { 
      return null; // null means no such item 
     } else { 
      return item.getName(); 
     } 
    } 
    public static double getPrice(String key) { 
     Item item = stock.get(key); 
     if (item == null) { 
      return -1.0; // negative price means no such item 
     } else { 
      return item.getPrice(); 
     } 
    } 
    public static int getQuantity(String key) { 
     Item item = stock.get(key); 
     if (item == null) { 
      return -1; // negative quantity means no such item 
     } else { 
      return item.getQuantity(); 
     } 
    } 

    public static void update(String key, int extra) { 
     Item item = stock.get(key); 
     if (item != null) { 
      item.quantity += extra; 
     } 
    } 
} 

這裏是一個不同的類,它是我的GUI的一部分,它看起來像:http://imgur.com/Jhc4CAz

和我的想法是你鍵入的項目如的代碼。 22然後輸入您想要添加到股票的數量,例如5點擊添加,這樣它會添加到變量中,但會立即更新框中的文本,就像您在屏幕上看到的那樣。

我真的對hashmap/list感到困惑我不認爲有一點將hashmap中的所有數據複製到列表中,並且將其乘以相當多,必須有更好的方法來實現這一點。

public class UpdateStock extends JFrame implements ActionListener { 

JTextField stockNo = new JTextField(4); 
JButton addButton = new JButton("ADD"); 
JSpinner quantitySlider = new JSpinner(); 
JTextArea catalog = new JTextArea(7, 30); 
List items = new ArrayList(); 

public UpdateStock(){ 
    setLayout(new BorderLayout()); 
    setBounds(100, 100, 450, 500); 
    setTitle("Update Stock"); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

    JPanel top = new JPanel(); 
    add("North", top); 
    JPanel middle = new JPanel(); 
    add("Center", middle); 

    top.add(stockNo); 
    top.add(quantitySlider); 
    top.add(addButton); 

    catalog.setLineWrap(true); 
    catalog.setWrapStyleWord(true); 
    catalog.setEditable(false); 
    middle.add(new JScrollPane(catalog)); 
    for(String key : StockData.getStock().keySet()) { 
     catalog.append("Item: " + key +"\n"); 
     items.add(StockData.getName(key)); 
     catalog.append("Name: " + StockData.getName(key) + 
       " Price: " + StockData.getPrice(key) + 
       " Qty: " + StockData.getQuantity(key)+"\n"); 
    } 
    setResizable(false); 
    setVisible(true); 
} 

}

+0

我沒有在您的其他文章上發佈完整的示例嗎? – JayC667

+0

@ JayC667我已經看到了這個例子,它對我來說是理解或者通過的方式,而且我沒有看到使用列表將數據從散列表複製到列表中,並且將存儲它所需的內存量加倍 – Higeath

+0

嗯......呃......實際上每個*對象引用只需要4個字節*,因爲實際的對象不會被複制。是的,這是相當大的,但它是一個非常乾淨和容易擴展的例子。這是基本的想法,有兩個不同的物品清單,因爲庫存和購物車** HAPPEN TO BE **兩個完全不同的清單,具有不同的*目的*。不要擔心那裏的內存大小,應用程序中至少有一百萬個這樣的對象,並且沒有任何內存問題。 – JayC667

回答

1

代碼立即將文本在JTextArea因爲你告訴它。這是正確的,在構造函數中:

for(String key : StockData.getStock().keySet()) { 
    catalog.append("Item: " + key +"\n"); 
    items.add(StockData.getName(key)); 
    catalog.append("Name: " + StockData.getName(key) + 
      " Price: " + StockData.getPrice(key) + 
      " Qty: " + StockData.getQuantity(key)+"\n"); 
} 

如果你想等到用戶設置的任何文本之前挑選的項目,然後用其addActionListener()方法上註冊一個addButtonActionListener。使用該監聽器的actionPerformed()方法來設置文本。別忘了從構造函數中刪除上面顯示的代碼。

我看你已經知道ActionListener這個類,因爲它是由UpdateStock實現的,但是這樣做有點奇怪(儘管完全有效!)。我不認爲我已經看到很多JFrame的子類直接實現它。通常的模式是使用anonymous ActionListener,而只是註冊。如果您真的想使用UpdateStock作爲ActionListener,那麼您需要在UpdateStock中定義的actionPerformed()方法,並且您需要將this註冊爲按鈕上的操作偵聽器。

+0

我希望它像現在這樣immidiately顯示所以Gorrila套裝是數量是7,所以有人類型22(大猩猩服裝的代碼)然後2並點擊添加,我想JTextArea得到immidiately更新,並顯示9而不是7這是我的問題,我不知道如何改變這個值 – Higeath

相關問題