当前位置: 首页 > 面试题库 >

如何确保JTextField仅包含字母?

芮瑾瑜
2023-03-14
问题内容

我只想在我的姓名字段中输入字母作为输入。

我已经尝试过使用matchs方法,但是不幸的是出了点问题,并且抛出了异常

还有其他检查方法吗?

   import java.awt.BorderLayout;
   import java.awt.FlowLayout;
   import java.awt.GridBagConstraints;
   import java.awt.GridBagLayout;
   import java.awt.GridLayout;
   import java.awt.Insets;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javax.swing.*;


    public class CreateAccount extends JFrame implements ActionListener{

    JPanel details = new JPanel(new GridBagLayout());

    JLabel fName= new JLabel("First Name:");
    JTextField firstNameField = new JTextField(10);

    JLabel lName= new JLabel("Last Name:");
    JTextField lastNameField = new JTextField(10);

    JLabel initialDeposit = new JLabel("Initial Deposit: ");
    JTextField initialDepositField = new JTextField(10);

    String accountTypes[] = {"Savings","Current"};

    JComboBox accountTypesComboBox = new JComboBox(accountTypes);
    JLabel accountType= new JLabel("Account type: ");

    JButton submit = new JButton("SUBMIT");
    JButton review = new JButton("REVIEW");

    Administrator admin = new Administrator();
    User u[] = new User[admin.maxNumberOfUsers];

    CreateAccount() {
        buildGui();
        setSize(400,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void initialiseUserCount() {
        admin.numberOfSavingsAccount = 0;
        admin.numberOfCurrentAccount = 0;
        admin.numberOfUsers=0;
    }
    public void buildGui() {

        setTitle("New Account Form");

        //JPanel submitPanel = new JPanel();
        //submitPanel.add(submit);


        GridBagConstraints c = new GridBagConstraints();
        c.insets=new Insets(10,10,10,10);
        // Stretch components horizontally (but not vertically) 
        c.fill = GridBagConstraints.HORIZONTAL;
        // Components that are too short or narrow for their space
        // Should be pinned to the northwest (upper left) corner
        c.anchor = GridBagConstraints.NORTHWEST;
        // Give the "last" component as much space as possible
        c.weightx = 1.0;

        c.gridx=0;
        c.gridy=0;
        details.add(fName,c);
        c.gridx=1;
        c.gridy=0;
        details.add(firstNameField,c);
        c.gridx=0;
        c.gridy=1;
        details.add(lName,c);
        c.gridx=1;
        c.gridy=1;
        details.add(lastNameField,c);
        c.gridx=0;
        c.gridy=2;
        details.add(initialDeposit,c);
        c.gridx=1;
        c.gridy=2;
        details.add(initialDepositField,c);
        c.gridx=0;
        c.gridy=3;
        details.add(accountType,c);
        c.gridx=1;
        c.gridy=3;
        details.add(accountTypesComboBox,c);
        c.gridx=0;
        c.gridy=4;
        details.add(submit,c);
        c.gridx=1;
        c.gridy=4;
        details.add(review,c);
        add(details);

        firstNameField.addActionListener(this);
        review.addActionListener(this);

    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==firstNameField) {
            try {
                String uFName = firstNameField.getText().toString();

                if(!uFName.matches("[A-Za-z]+"))
                    throw new Exception();
            }
            catch(Exception e1) {
                firstNameField.setText("");
                JOptionPane.showMessageDialog(firstNameField,"Please enter a valid name!");
            }
        }
    }
}

问题答案:

您可以尝试使用此正则表达式

if(!uFName.matches("^[a-zA-Z]+$"))


 类似资料:
  • 我有一个POJO,我想从JSON创建这个类的一个实例。我使用jackson将JSON转换为Object。我想确保JSON将控制我的POJO的所有属性。JSON可能包含其他额外字段,但必须包含POJO的所有属性。 例子: JSON#1 JSON#2 在这里,我希望JSON#1成功转换为MyClass,但JSON#2应该失败。我该怎么做?这有注释吗?

  • 问题内容: 我正在尝试验证我的字符串是否与模式匹配。也就是说,完整的字符串可以写为该模式。但是,如果有任何子字符串匹配该模式,则返回。(如回报,这是我不想在这个例子中,我宁愿验证,整个字符串只含有少量拉丁字母)。 问题答案: 您可以使用 开始 和 结束 标记,并分别指示开始,在您的正则表达式模式的字符串的结尾。这样,您可以使表达式仅匹配整个字符串,而不匹配任何子字符串。在您的情况下,它将如下所示:

  • 问题内容: 我有这个模特 我希望用户在两个字段中都不能使用字母数字以外的任何其他字符。 有什么办法吗? 问题答案: 您将使用验证器来限制字段接受的内容。A可以在这里实现窍门: 请注意,已经有一个验证器可以为您验证电子邮件地址。上述验证器将不允许使用有效的电子邮件地址。

  • 下面的查询正在从关系表中收集内容ID。它是确保我的结果有至少2个关键字匹配。 我的问题是,我也有一个“硬关键字”(假设ID 127),这意味着我需要确保硬关键字是必须包含的。我想我需要修改我的子句,但我不知道如何修改。

  • 我有一个文本字段(名称),我想防止用户输入数字,或为空! 我尝试了所有这些,但其中有一些缺陷:

  • 问题内容: 我需要一个可以告诉我字符串是否包含非字母数字字符的方法。 例如,如果字符串为“ abcdef?” 或“abcdefà”,该方法必须返回true。 问题答案: 使用Apache Commons Lang: 另一种方法是遍历String的字符并检查: 您还剩下一个问题:示例字符串“abcdefà”是字母数字,因为是字母。但我认为您希望将其视为非字母数字,对吗? 因此,您可能想使用正则表达式