我使用此代码来确保来自文本字段的每个给定数据都是正确的。
但是在 dateTextField 中,当我给它任何内容或将其留空时,会显示其错误消息,但是当我单击保存按钮时,该数据将保存到我的 JTable 中!
我的错误在哪里?
public class AddBookDialog extends javax.swing.JDialog {
public AddBookDialog(JFrame owner){
super(owner,"New Book",true);
...
}
BookInformation bookinform;
private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {
bookinform=new BookInformation();
if(!BookIDControl()){
return;
}
if(!BookNameControl()){
return;
}
if(!BookDateControl()){
return;
}
bookinform.setBorrowStatus("No");
AddBookDialog.this.dispose();
}
public BookInformation getBookInfos() throws NullPointerException{
return bookinform;
}
public boolean BookNameControl(){
bookinform.setBookName(BookNametextfiled.getText());
if(bookinform.getBookName().trim().isEmpty()){
errorLable.setText("Empty Book Name");
return false;
}
if(havedigit(bookinform.getBookName().trim())){
errorLable.setText("Book Name Have Digit");
return false;
}
return true;
}
public boolean BookDateControl(){
bookinform.setBookDate(BookDatetextfield.getText().trim());
if(bookinform.getBookDate().trim().isEmpty()){
errorLable.setText("Empty Book Date");
return false;
}
else if(haveSpace(bookinform.getBookDate().trim())){
errorLable.setText("Space in Book Date!");
return false;
}
else if(haveletter(bookinform.getBookDate().trim())){
errorLable.setText("Letter in Book Date");
return false;
}
return true;
}
public boolean BookIDControl(){
bookinform.setBookID(BookidTextfield.getText().trim());
if(bookinform.getBookID().trim().isEmpty()){
errorLable.setText("Empty Book ID");
return false;
}
if(haveSpace(bookinform.getBookID().trim())){
errorLable.setText("Space in Book ID !");
return false;
}
if(haveletter(bookinform.getBookID().trim())){
errorLable.setText("Letter in Book ID");
return false;
}
return true;
}
...
二等:
public class AllBooksM extends JFrame implements ActionListener{
...
if(e.getSource()==AddBookButton){
AllBooks allBooks=new AllBooks();
AddBookDialog add_book=new AddBookDialog(AllBooksM.this);
add_book.setVisible(true);
BookInformation B_info=add_book.getBookInfos();
if(B_info != null){
allBooks.AddRow(B_info);
}
// bookcount();
}
...
}
我的输出:
26 thired 62 Yes
29 sixth 92 No
35 vff 53 No
34332 dsds null
3434 ssdas null
222 fgfgf null
77 sds null