简体   繁体   English

如何在Java中每次使用JTextField将值输入数组

[英]How do i input a value into an array using a JTextField each time in Java

i am creating a program in which the user will add the name of someone into the textfield however i created the array which is String[] studentNames; 我正在创建一个程序,用户可以在其中将某人的名字添加到文本字段中,但是我创建了一个数组,它是String[] studentNames; but when the user presses submit it will add the name to the array but i want it so that each new text entry will be added to the array elements individually? 但是当用户按下提交时,它将名称添加到数组,但是我想要它,以便每个新的文本条目将被单独添加到数组元素?

You should use ArrayList instead of Array, where you would have not fixed-size list. 您应该使用ArrayList而不是Array,因为这里没有固定大小的列表。

List<String> list = new ArrayList<String>();

// ...

button.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String text = textField.getText();
        list.add(text);
    }
});

You should create a static field String[] studentNames in a class and in the ActionListener of your submit button update that array ever time the user inserts a name, 您应该在类中创建一个静态字段String [] studentNames,并在用户每次输入名称时,在“提交”按钮的ActionListener中更新该数组,

If you don't know the number of names the user will insert prior to allocating the array, you should use an ArrayList 如果您不知道用户在分配数组之前要插入的名称数量,则应使用ArrayList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将函数作为Java中JTextField的输入? - How do I take a function as an input from a JTextField in Java? java-如何在JTextField中显示字符数组 - java - How do I display a character array in a JTextField 如何在Java中从JTextField设置Bean属性值? - How do i set a Bean property value from JTextField in Java? 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 每当用户输入字符串值时,如何自动生成整数值? - How do I auto-generate an integer value for each time a user input a string value? 我如何限制我想要在 Jtextfield 中输入的长度? - how do i limit the length of the input i want inside a Jtextfield? 如何从通过JLabel单击动态创建的Java中的JTextField中获取值 - How do i get the value from JTextField in java that was created dynamically through a JLabel click 在Java中,如何避免空白的jtextfield输入? - In Java, how can I avoid blank jtextfield input? 如何获取“JTextField 数组”的值并将其存储在 Array Integer 中? - How can I get the value of the 'Array of JTextField' and store it in an Array Integer? 如何从JTextField获取输入,然后将其存储在变量中? - How Do I Get Input From A JTextField, Then Store It In A Variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM