在需要填充空字段时进行无限循环



我一直在试图处理我的程序中可能出现的错误,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);
}

由于它位于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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新