从一个JTextField java中一个接一个地获取输入

  • 本文关键字:一个 获取 JTextField java java
  • 更新时间 :
  • 英文 :


我是Java的新手,我正在研究书店的基本表示,用户可以在其中将书籍,教科书或工作簿添加到书店(数组列表)。为此,我必须使用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("");
            }
        }

每当按下键盘上的 Enter 键时,JTextField 的 actionPerforming 事件就会触发。这使得它非常适合在处理用户键入的内容之前接受用户的完整输入。考虑到这一点,在此事件中将输入文本转换为整数是一个非常糟糕的主意

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);
    }
}

你会明白的。由你来完成它。

最新更新