从一个JTextField java中依次获取输入

时间:2017-11-04 19:48:39

标签: java

我是Java的新手,我正在研究本应该是书店的基本代表,用户可以在书店中添加书籍,教科书或工作簿(arraylist)。我必须为此使用GUI,并且允许的唯一元素是JTextArea和JTextField。我的问题是在actionlistener中,文本字段中的初始输入只是成为其后的其他提示的输入,而不允许用户放入任何内容。有没有办法解决这个问题?下面是我的代码片段。

public static void main(String[] args){

    BookStore b = new BookStore();
    b.setVisible(true);

}
String input;
public BookStore() {

    super("Bookstore");
    setSize(800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(1, 2));

    textField = new JTextField(25);
    panel = new JPanel();
    textField.addActionListener(new ActionListener(){
        @Override 
        public void actionPerformed(ActionEvent ae) {
            /*
                The majority of your code should be written here.
                Note that this scope has access to the fields textField
                and textArea.
            */
            //input = textField.getText();

            if(Integer.parseInt(textField.getText()) == 1) {
                bookSubMenu();
                textField.setText(null);
                input = textField.getText(); //this just uses the input already present in the JTextField; how can i fix that
                if(Integer.parseInt(input) == 1) {
                    textArea.append("Enter the title of the book: ");
                    tempTitle = textField.getText();//This also uses the input from the original getText() without allowing any input to be entered
                    textArea.append(tempTitle);
                    ret = addBook(BT_BOOK, tempTitle);
                }
                //getBookInfo();
                //textField.setText("");
            }
        }

1 个答案:

答案 0 :(得分:0)

只要点击键盘上的 Enter 键,就会触发JTextField的 actionPerformed 事件。这使得它非常适合在处理他/她键入的内容之前接受用户的完整输入。考虑到这一点,在此事件中将输入文本转换为整数是一个坏主意

if(Integer.parseInt(textField.getText()) == 1) { ... }

除非您确定用户确实输入了整数数值,因为字母字符串肯定会产生数字格式异常。在您执行此类操作之前,您需要确保该条目对转换有效,可能是这样的:

input = textField.getText();

// If nothing was entered.
if (input.trim().equals("") || input.isEmpty()) {
    textField.setText(null); // In case only whitespaces were entered.
    // Get out of the event. No sense wasting time here.
    return;
}

// Use the String.matches() method with a Regular Expression.
// The "\\d+" checks to see if everything within the textfield
// is a string representation of a integer numerical value.
if (input.matches("\\d+")) { 

    // CODE RELATED TO NUMERICS GOES HERE
}
else {
    // CODE RELATED TO NON-NUMERICS GOES HERE
}

使用布尔变量作为标志或整数可以设置(更好)菜单类型选择变量,以便您始终知道TextField条目的具体内容。它可能会是这样的:

public void actionPerformed(ActionEvent ae) {
    input = textField.getText();

    // If nothing was entered.
    if (input.trim().equals("") || input.isEmpty()) {
        textField.setText(null); // In case only whitespaces were entered.
        // Get out of the event. No sense wasting time here.
        return;
    }

    // Use the String.matches() method with a Regular Expression.
    // The "\\d+" checks to see if everything within the textfield
    // is a string representation of a integer numerical value.
    if (input.matches("\\d+")) {
        // The following Integer variable is declared as class global
        // and initialized to 0 (same place the String variable named 
        // input was declared).
        menuItem = Integer.parseInt(textField.getText());

        // Note: You can use swicth/case for the following...
        if(menuItem == 1) { 
            // Code goes here if 1 was entered
            textArea.append("Enter the title of the book: ");
        }
        else if(menuItem == 2) { 
            // Code goes here if 2 was entered.
        }
        // etc, etc...
        else {
            JOptionPane.showMessageDialog(null, "Invalid Menu Entry Provided!", 
                                "Invalid Entry", JOptionPane.WARNING_MESSAGE);
            menuItem = 0;
        }
        textField.setText(null);
    }

    // Non-Numerical String entered.
    else {
        // Process String data here.
        // Note: You can use swicth/case for the following...
        if (menuItem == 1) {
            tempTitle = textField.getText();
            textArea.append(tempTitle + System.lineSeparator());
            ret = addBook(BT_BOOK, tempTitle);
        }
        else if (menuItem == 2) {
            // If menuItem 2 is currently active.
        }
        // etc, etc...
        else {
            JOptionPane.showMessageDialog(null, "Invalid Entry Provided!\nUndetermined location for content!", 
                                "Invalid Entry", JOptionPane.WARNING_MESSAGE);
            menuItem = 0;
        }
        textField.setText(null);
    }
}

你会明白这个想法。它取决于你完成它。