存储多个文本字段输入的java
我一直在做一个项目,试图通过GUI上的用户输入将输入存储在阵列中。 现在的问题是我找不到类似于Scanner方法的“input.next”的东西
谢谢你的帮助
下面是从我的Main中执行的GUI类的样子
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class GUI implements ActionListener {
//Private the variables to make them accessible to other methods
private static JLabel orderLabel;
private static JTextField orderText;
private static JButton confirmButton;
public GUI(){
JFrame window = new JFrame();
JPanel panel = new JPanel();
window.setSize(350, 200);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.add(panel);
window.setTitle("Place your order");
panel.setLayout(null);
//Creating label for the order text field:
orderLabel = new JLabel("Your order:"); //Creates the label memory
orderLabel.setBounds(10, 20, 80, 25); //Creates bounds for the label
panel.add(orderLabel); //Adds the label to the window
//Creating the text field
orderText = new JTextField(); //Creates text field memory
orderText.setBounds(100, 22, 165, 25); //Creates text field bounds
orderText.addActionListener(this);
panel.add(orderText); //Adds the text field to the window
//Creating the button to confirm order
confirmButton = new JButton("Confirm order");
confirmButton.setBounds(90, 100, 170, 30);
confirmButton.addActionListener(this);//Links the button to the action listener method inside this file
panel.add(confirmButton);
window.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
List<String> list = new ArrayList<String>();
String input = orderText.getText();
String temp = input.next();
}
}
# 1 楼答案
如果要对JTextField的内容使用Scanner#next()方法,只需使用Scanner对象即可,例如:
当然,您希望JTextField中提供的所有内容都用空格分隔,例如:
这将显示为如下列表:
当然,这可能不够好,因为很多必填项都包含多个单词,例如:
这将显示为如下列表:
不是真正想要的。在这种特殊情况下,可以使用Scanner#useDelimiter()方法指定在JTextField中键入所需项时计划使用的分隔符。如果你决定用分号(;)作为输入项和输入JTextField之间的分隔符:
这个列表看起来更像:
代码是:
要将其放入(某种)可运行的应用程序中: