简体   繁体   English

我如何使用Jtextfield在对象数组中命名所有对象

[英]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(); ) 现在,我希望用户输入的名称productJTextField (使用类似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.

相关问题 我如何透明我的JTextField和JLabel - how can i transparent my JTextField and JLabel 如何创建JTextField数组? - How can I create a JTextField array? 如何让我的程序在 JtextField 中显示数据库中的用户名? - How can I make my program display the user's name from the database in JtextField? 如何将JButton连接到JTextField来存储输入到JTextField中的int? - How can I connect my JButton to my JTextField to store the int I input into the JTextField? 如何获取“JTextField 数组”的值并将其存储在 Array Integer 中? - How can I get the value of the 'Array of JTextField' and store it in an Array Integer? 如何根据 JCheckBox 的状态启用/禁用我的 JTextField? - How can I enable/disable my JTextField depending of the state of a JCheckBox? 如何重置JTextField…? - How can I reset JTextField…? 读取struts-config.xml文件时,struts控制器会创建动作对象吗? - is struts controller creats the action objectes when reading struts-config.xml file? 如何通过循环或其他方式对单个 JTextField 中放置在数组中的所有值求和以获得均值或平均值? - How do I sum all the values placed in an array from a single JTextField by looping or otherwise to get the mean or average? 如何从数组中为 Jtextfield 设置文本? - How can set text for Jtextfield from the array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM