[JAVA无限循环,同时需要填充空字段

时间:2012-12-28 02:45:12

标签: loops while-loop field fill infinite

我一直在尝试使用while循环显示消息对话框中的“填充容器”来处理程序中可能出现的错误,但是这样做是无限的。简单地说,当我尝试添加某些内容(例如联系人)时,必须填写一些字段,我该如何确定?

这是我的代码,很适合找到未填充的字段,但它无限运行!

    public void createContact(){
    final JFrame addContact = new JFrame("Add Contact");
    addContact.setSize(400,300);
    addContact.setLayout(new BorderLayout());
    addContact.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JPanel info = new JPanel(new GridLayout(6,2));
    JButton add = new JButton("Add");
    JLabel title = new JLabel("Title: ");
    JLabel firstname = new JLabel("FIRST NAME: ");
    JLabel lastname = new JLabel("LAST NAME: ");
    JLabel adress = new JLabel("Adress: ");
    JLabel email = new JLabel("Email: ");
    JLabel company = new JLabel("Company Name: ");
    final JTextField title1 = new JTextField(20);
    final JTextField firstname1 = new JTextField(20);
    final JTextField lastname1 = new JTextField(20);
    final JTextField adress1 = new JTextField(20);
    final JTextField email1 = new JTextField(20);
    final JTextField company1 = new JTextField(20);
    info.add(title);
    info.add(title1);
    info.add(firstname);
    info.add(firstname1);
    info.add(lastname);
    info.add(lastname1);
    info.add(adress);
    info.add(adress1);
    info.add(email);
    info.add(email1);
    info.add(company);
    info.add(company1);
    addContact.add(info,BorderLayout.NORTH);
    addContact.add(add,BorderLayout.SOUTH);

    add.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            while(title1.getText().length() == 0 || firstname1.getText().length() == 0 || firstname1.getText().length() == 0 || adress1.getText().length() == 0 || email1.getText().length() == 0 ||company1.getText().length() ==0){
            JOptionPane.showMessageDialog(null,"Please fill the empty fields.");
            }
            Contact newContact = new Contact(title1.getText(),firstname1.getText(),lastname1.getText(),adress1.getText(),email1.getText(),company1.getText());
            contactList.add(newContact);
            JOptionPane.showMessageDialog(null,"Contact has been added successfuly!");
            addContact.setVisible(false);

        }
    });
    addContact.pack();
    addContact.setVisible(true);

}

1 个答案:

答案 0 :(得分:0)

由于它处于while块中,因此它将始终连续运行(因为始终满足条件)。但你不能修改任何东西,因为一旦你确认消息,我会再次弹出。请尝试使用'if'代替:

add.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
        if (title1.getText().length() == 0 || firstname1.getText().length() == 0 || firstname1.getText().length() == 0 || adress1.getText().length() == 0 ||    email1.getText().length() == 0 ||company1.getText().length() ==0){
            JOptionPane.showMessageDialog(null,"Please fill the empty fields.");
        } else {
            Contact newContact = new Contact(title1.getText(),firstname1.getText(),lastname1.getText(),adress1.getText(),email1.getText(),company1.getText());
            contactList.add(newContact);
            JOptionPane.showMessageDialog(null,"Contact has been added successfuly!");
            addContact.setVisible(false);
        }
    }
}