有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

存储多个文本字段输入的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) 个答案

  1. # 1 楼答案

    如果要对JTextField的内容使用Scanner#next()方法,只需使用Scanner对象即可,例如:

    List<String> list = new ArrayList<>();
    Scanner input = new Scanner(orderText.getText());
    while (input.hasNext()) {
        list.add(input.next());
    }
    

    当然,您希望JTextField中提供的所有内容都用空格分隔,例如:

    Bread Milk Sugar Coffee
    

    这将显示为如下列表:

    Bread
    Milk
    Sugar
    Coffee
    

    当然,这可能不够好,因为很多必填项都包含多个单词,例如:

    French Fries Whole Wheat Bread 2% Milk
    

    这将显示为如下列表:

    French
    Fries
    Whole
    Wheat
    Bread
    2%
    Milk
    

    不是真正想要的。在这种特殊情况下,可以使用Scanner#useDelimiter()方法指定在JTextField中键入所需项时计划使用的分隔符。如果你决定用分号(;)作为输入项和输入JTextField之间的分隔符:

    French Fries; Whole Wheat Bread; 2% Milk
    

    这个列表看起来更像:

    French Fries
    Whole Wheat Bread
    2% Milk
    

    代码是:

    List<String> list = new ArrayList<>();
    Scanner input = new Scanner(orderText.getText());
    input.useDelimiter("\\s{0,};\\s{0,}");
    while (input.hasNext()) {
        list.add(input.next());
    }
    

    要将其放入(某种)可运行的应用程序中:

    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class GUI extends JFrame implements ActionListener {
    
        private static final long serialVersionUID = 1L;
    
        //Private the variables to make them accessible to other methods
        private static JLabel orderLabel;
        private static JTextField orderText;
        private static JButton confirmButton;
    
        public GUI() {
            initializeForm();
        }
    
        private void initializeForm() {
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(350, 200);
            setAlwaysOnTop(true);
            setTitle("Place your order");
            setLocationRelativeTo(null);
    
            JPanel panel = new JPanel();
            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.setActionCommand("orderText");
            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.setActionCommand("confirmButton");
            confirmButton.setBounds(90, 100, 170, 30);
            confirmButton.addActionListener(this);//Links the button to the action listener method inside this file
            panel.add(confirmButton);
            add(panel);
        }
    
        public static void main(String[] args) {
            new GUI().setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            String textToTokenize = orderText.getText().trim();
            // TRY to determine the delimiter used within the JTextField entry
            String delimiter = textToTokenize.replaceAll("[^:;'\"\\|\\\\~!@#$%\\^&\\"
                                                       + "*-_+=<>.?/]", "").trim();        
            if (delimiter.length() == 0) {
                delimiter = " ";
            }
            else {
                delimiter = delimiter.substring(0, 1);
            }
        
            if (!textToTokenize.isEmpty() && 
                        (e.getActionCommand().equals("confirmButton") || 
                        e.getActionCommand().equals("orderText"))) {
                List<String> shoppingList = new ArrayList<>();
                // If you want to use Scanner's next() method then 
                // just use Scanner to do it...
                Scanner input = new Scanner(textToTokenize);
                input.useDelimiter("\\s{0,}" + delimiter + "\\s{0,}");
                while (input.hasNext()) {
                    shoppingList.add(input.next());
                }
                // Test...Display list in console window:
                JOptionPane.showMessageDialog(this, "<html>Supplied shopping list items are:"
                                    + "<br><br><center><font color=blue>" + shoppingList.toString()
                                    .replaceAll("[\\[\\]]", "") + "</font></center><br></html>", 
                                    "Supplied List...", JOptionPane.INFORMATION_MESSAGE);
            }
            orderText.setText("");
            orderText.requestFocus();
        }
    }