[英]how i can name all my objectes in array of objectes with Jtextfield
http://i.stack.imgur.com/68ou3.png I have a class named trader
and I have another class named product
. http://i.stack.imgur.com/68ou3.png我有一个名为
trader
的类,还有另一个名为product
类。
My task is: 我的任务是:
the trader should buy many products (an object array of class
product
)交易者应该购买许多产品(类
product
的对象数组)
Now I want the user to enter the name of the product
in a JTextField
(using something like Product k=new Product();
) 现在,我希望用户输入的名称
product
在JTextField
(使用类似Product k=new Product();
)
I want the JTextField to find the corresponding object from the product
objects (somehow using/implementing the equals(k)
method) and stored in the array of objects he has bought ( product
) in the trader
every time he adds a product this way. 我希望JTextField从
product
对象(以某种方式使用/实现equals(k)
方法)中找到对应的对象,并存储在他每次以这种方式添加产品时在trader
购买的产品( product
)的对象数组中。
How can this be implemented? 如何实现呢?
You can't expect the user to enter the variable name of an object to the JTextField and add that object accordingly. 您不能期望用户将对象的变量名称输入JTextField并相应地添加该对象。 Even if you can do it by reflection, it is not to be used in that manner.
即使可以通过反射进行操作,也不能以这种方式使用它。
You can perhaps create a Product
class with a name
attribute within the Product
class: 您也许可以在
Product
类中创建一个具有name
属性的Product
类:
class Product
{
private String name;
//Your other attributes here..
public Product(String name){
this.name = name;
}
}
As the user enters the name of the product into the textfield and click the button, you can add the product according to its name as such: 当用户在文本字段中输入产品名称并单击按钮时,您可以根据产品名称添加产品,如下所示:
@Override
actionPerformed(ActionEvent e){
traderList.add(new Product(txtProduct.getText()));
}
The traderList can be an arrayList which stores various products. traderList可以是存储各种产品的arrayList。 You may also add some checks on the product name before adding them to the list.
您还可以在产品名称上添加一些检查,然后再将其添加到列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.