[英]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.