2013-03-19 14 views
0

我要如何單獨的一個字符串使用, 提取碼如何將字符串轉換成大量的單子,

JTextField stockNo = new JTextField(7); 
JTextField quantity = new JTextField(7); 
TextArea information = new TextArea(5, 50); 
JButton purchaseBtn = new JButton("Purchase"); 
purchaseBtn.addActionListener(this); 
//set up gui ect 
String key = stockNo.getText(); 
String quantityTxt = quantity.getText(); 
String info = "\n\nName: " + StockData.getName(key) + 
        "\nPrice: £" + (StockData.getPrice(key)) + 
        "\nNumber in stock: " + StockData.getQuantity(key) + 
        "\nNumber to buy: " + quantityTxt; 
information.append(info); 

當用戶點擊purchaseBtn,信息應保持爲此在.append之前,我需要的是以某種方式從信息文本區域提取信息,並用它來計算,有人告訴我數組列表,但不知道。

//in text area 
Name: shirt //user inputed 1 for key 
Price: £5.00 
Number in stock: 15 
Number to buy 7 // user inputed 7 for quantity 

Name: socks //user inputed 2 for key 
Price: £2.50 
Number in stock: 4 
Number to buy 1 // user inputed 7 for quantity 

Name: trouser //user inputed 3 for key 
Price: £24.00 
Number in stock: 19 
Number to buy 4 // user inputed 7 for quantity 

我需要把它保存每一行的關鍵和數量,這樣我就可以用它以後做的事情,並且能夠說int totalcost = //the price of all of them added together in our example £31.50

+0

有什麼問題請參見How to use Lists?我們不會爲你寫代碼。你遇到什麼具體的部分? – tjameson 2013-03-19 23:50:06

+0

你的問題沒有一個可以理解。 – 2013-03-19 23:50:08

回答

0

正在操作以OO語言。你所有的信息都應該在Object中管理,這就是關鍵。使用JList而不是JTextArea

放置在一個Purchase對象攜帶所有你所需要的信息,如nameprice,的inStock and toBuy`您的購買信息。

然後,您可以根據需要使用ListCellRenderer呈現Purchase對象的各個元素。

這將使它更容易管理。

瞭解更多信息

+0

非常感謝你看看它 – user2158735 2013-03-19 23:59:00

+0

https://www.dropbox.com/s/e8cwysszjb5544n/puchaseitem.zip?m代碼你請看看它,我不能一起加入它, – user2158735 2013-03-21 00:04:31