我有一个定制的JPanel itemSmallCard(String prodID, String productName, String price, String retailer)
,我使用它在另一个JPanel中创建了它的多个实例,在按钮的ActionPerformed事件上使用不同的参数,如下所示:
JPanel iC = new itemSmallCard("123456789ab", "Inspiron 7567 Intel Core i5 DELL Laptop", "48957", "Cloudtail Pvt. Ltd.");
mainContentPane.add(iC);
mainContentPane.revalidate();
mainContentPane.repaint();
但是,由于所有新的JPanels都是用相同的代码使用相同的变量iC
创建的,我不知道如何从它们访问特定的JPanel变量,因此也无法访问它的事件侦听器。我怎样才能做到这一点呢?
发布于 2019-05-31 01:59:42
也许可以创建一个ArrayList (如果您不需要访问不基于位置的特定don)。
ArrayList<JPanel> panels = new ArrayList<JPanel>();
或者,如果您需要访问它们,您可以创建一个"HashMap“并将您的ID存储在其中。
WeakHashMap<String, JPanel> panels = new WeakHashMap<String, JPanel>();
panels.put("123456789ab", new itemSmallCard("123456789ab", "Inspiron 7567 Intel Core i5 DELL Laptop", "48957", "Cloudtail Pvt. Ltd.");// Adds a product with that info
panels.get("123456789ab"); // Returns the panel with that ID.
希望这能有所帮助。
https://stackoverflow.com/questions/56383205
复制相似问题