如何将字符串分开使用,代码摘录
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 = "nnName: " + 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
,
您正在使用OO语言进行操作。你所有的信息都应该在对象中管理,这才是重点。
用JList
代替JTextArea
将您的购买信息放置在Purchase
对象中,该对象包含您需要的所有信息,例如name
, price,
inStock and
toBuy ' .
然后,您可以使用ListCellRenderer
根据需要呈现Purchase
对象的各个元素。
这将使它更容易管理。
参见如何使用列表了解更多信息