如何在java中验证文本字段

时间:2013-04-23 13:51:01

标签: java swing user-interface jtextfield

你好我需要一些帮助我的gui文本字段。我的教授给了我一个关于如何做的提示,但它冻结了我的程序。我尝试过其他方法,但如果我输入文本字段,则下一个窗口不显示。

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.*;

//import statements here
public class UserWindow extends JFrame
{

    private JTextField nameField, ageField, creditCardField;
    private JButton backButton, nextButton;

    public UserWindow()
    {
        super("Please enter your information");

        setSize(700,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(4,2));

        buildPanel();
        setVisible(true);

    }

    private void buildPanel(){

        nameField = new JTextField(10);
        ageField =  new JTextField(2);
        creditCardField = new JTextField(10);

        backButton = new JButton("Back");
        nextButton = new JButton("Next");
        nextButton.addActionListener(new NextButton());
        backButton.addActionListener(new BackButton());

        JLabel NameLabel =  new JLabel("Please enter your name");

        JLabel ageLabel = new JLabel("Enter your age");

        JLabel creditCardLabel = new JLabel("Enter your credit card number");

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
        JPanel panel6 = new JPanel();
        JPanel panel7 = new JPanel();
        JPanel panel8 = new JPanel();

        add(panel1);
        add(panel2);
        add(panel3);
        add(panel4);
        add(panel5);
        add(panel6);
        add(panel7);
        add(panel8);

        panel1.add(NameLabel);
        panel2.add(nameField);
        panel4.add(ageField);
        panel6.add(creditCardField);
        panel3.add(ageLabel);
        panel5.add(creditCardLabel);    
        panel7.add(backButton);
        panel8.add(nextButton);

    }//end of panel building

    //action listeners for fields/buttons

    private class NextButton implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String str;
            int age;
            str = nameField.getText();

            if (str.equals("")){
                JOptionPane.showMessageDialog(null,"Please enter your name.");

                while(true){
                    nameField.requestFocusInWindow();
                    if(!str.equals(""))
                        break;
                }
            }

            if(e.getSource() == nextButton)
                new MovieSelection();
            setVisible(false);

        }
    }
    private class BackButton implements ActionListener{
        public void actionPerformed(ActionEvent e){

            if (e.getSource() == backButton)
                setVisible(false);
            new SelectUserWindow();
        }
    }



}

2 个答案:

答案 0 :(得分:2)

如果最初为空字符串,则永远不会为str分配新值。

我认为你需要像

这样的东西
str = nameField.getText();

while(true)圈内。

答案 1 :(得分:1)

这很冷,因为你有这个:

if (str.equals("")){
     JOptionPane.showMessageDialog(null,"Please enter your name.");
     while(true){ //<- Runs forever
          nameField.requestFocusInWindow();
          if(!str.equals(""))
               break;
     }
}

当他说您在进入循环(或str)后从未向if分配任何值时,AcId是正确的。这意味着您永远不会更改str的值,它永远不会等于"",它将永远在循环中运行。循环在与用户界面相同的线程中运行,因此用户界面没有时间做任何其他事情(因此它是冻结的)。

在这种情况下拥有一个永远运行的循环是没有意义的。您似乎正在尝试让GUI忙等到用户输入名称。 不要那样做。而是检查用户是否输入了某些内容,如果没有,则提醒用户并等待用户再次单击该按钮。

所以删除循环并使用简单的if-else语句。

if (str.equals("")){  //User have not entered anything. 
    JOptionPane.showMessageDialog(null,"Please enter your name.");
    nameField.requestFocusInWindow();
    //Do NOT loop here.
}
else { 
    //Do everything you need to do when the user have entered something
}