0

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

}
4

1 回答 1

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);
        }
    }
}
于 2012-12-28T03:20:57.097 回答