下面是创建分别具有代码名称、价格和数量的商品的代码。
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 的一部分,如下所示:/image/fJ7R5.jpg
我的想法是你输入一个项目的代码,例如。 22 然后输入您想要添加到库存中的数量,例如 5,您单击“添加”,这样它就会添加到变量中,但会立即更新框中的文本,如您在屏幕上看到的那样。
我真的对 HashMap /列表感到困惑,我认为将所有数据从 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);
}
}
最佳答案
您的代码立即将文本放入 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");
}
如果您想等到用户选择一个项目后再设置任何文本,则注册 ActionListener
上addButton
使用其 addActionListener()
方法。使用该监听器的 actionPerformed()
设置文本的方法。不要忘记从构造函数中删除上面显示的代码。
我发现您已经知道 ActionListener
类,因为它是由 UpdateStock
实现的,但这样做有点奇怪(尽管完全有效!);我认为我没有见过 JFrame
的很多子类直接实现。通常的模式是使用 anonymous ActionListener
只需注册即可。如果你真的想使用UpdateStock
作为ActionListener
,那么你需要一个actionPerformed()
UpdateStock
中定义的方法您需要注册this
作为按钮上的 Action 监听器。
关于java - 使用hashmap显示JTextField中的数据并更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934283/