Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops [JAVA无限循环,同时需要填充空字段_Loops_While Loop_Field_Fill_Infinite - Fatal编程技术网

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

Loops [JAVA无限循环,同时需要填充空字段,loops,while-loop,field,fill,infinite,Loops,While Loop,Field,Fill,Infinite,我一直在尝试用while循环显示消息对话框来处理程序中可能出现的错误,对话框上写着“填充空格”,但它是无限的。简单地说,当我尝试添加某些内容(例如联系人)时,有些字段必须全部填充,我如何确保这一点 这是我的代码,可以很好地查找未填充的字段,但它可以无限运行 public void createContact(){ final JFrame addContact = new JFrame("Add Contact"); addContact.setSize(400,300)

我一直在尝试用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);
        }
    }
}

哇,那很简单!我还没想过,现在看起来很傻哈哈!谢谢!