我想比較驗證部分的JTextField中的值,以確保如果所有TextFields都沒有值,用戶將無法保存進度。Java,JTextField .equals
這是我的文本字段的方法。
public void DrawBookAttributes() {
lblBookName = new JLabel("Book Name :");
lblBookName.setFont(getLblFont());
lblBookName.setSize(140, 20);
lblBookName.setLocation(280, 150);
menuFrame.add(lblBookName);
tfBookName = new JTextField();
tfBookName.setSize(140, 20);
tfBookName.setLocation(420, 150);
menuFrame.add(tfBookName);
lblBookID = new JLabel("Book ID :");
lblBookID.setFont(getLblFont());
lblBookID.setSize(140, 20);
lblBookID.setLocation(280, 50);
menuFrame.add(lblBookID);
tfBookID = new JTextField();
tfBookID.setSize(140, 20);
tfBookID.setLocation(420, 50);
tfBookID.setText("hello");
tfBookID.setEditable(false);
menuFrame.add(tfBookID);
JLabel lblBookCost = new JLabel("Cost (£) :");
lblBookCost.setFont(getLblFont());
lblBookCost.setSize(140, 20);
lblBookCost.setLocation(280, 200);
menuFrame.add(lblBookCost);
tfBookCost = new JTextField();
tfBookCost.setSize(140, 20);
tfBookCost.setLocation(420, 200);
tfBookCost.addKeyListener(this);
menuFrame.add(tfBookCost);
lblBookYearOfPublication = new JLabel("Publication Date :");
lblBookYearOfPublication.setSize(140, 20);
lblBookYearOfPublication.setLocation(280, 250);
lblBookYearOfPublication.setFont(getLblFont());
menuFrame.add(lblBookYearOfPublication);
JTextField tfBookYearOfPublication = new JTextField();
tfBookYearOfPublication.setSize(140, 20);
tfBookYearOfPublication.setLocation(420, 250);
tfBookYearOfPublication.setText("DD-MM-YYYY");
tfBookYearOfPublication.addActionListener(this);
menuFrame.add(tfBookYearOfPublication);
JLabel lblbookPublishngHouse = new JLabel("Publishing Orgaisation :");
lblbookPublishngHouse.setSize(140, 20);
lblbookPublishngHouse.setLocation(280, 300);
lblbookPublishngHouse.setFont(getLblFont());
menuFrame.add(lblbookPublishngHouse);
JTextField tfBookPublishingHouse = new JTextField();
tfBookPublishingHouse.setSize(140, 20);
tfBookPublishingHouse.setLocation(420, 300);
menuFrame.add(tfBookPublishingHouse);
JLabel lblBookISBN = new JLabel("ISBN :");
lblBookISBN.setSize(140, 20);
lblBookISBN.setLocation(280, 350);
lblBookISBN.setFont(getLblFont());
menuFrame.add(lblBookISBN);
JTextField tfBookISBN = new JTextField();
tfBookISBN.setSize(140, 20);
tfBookISBN.setLocation(420, 350);
menuFrame.add(tfBookISBN);
JLabel lblBookPages = new JLabel("Number of Pages :");
lblBookPages.setSize(140, 20);
lblBookPages.setLocation(280, 400);
lblBookPages.setFont(getLblFont());
menuFrame.add(lblBookPages);
JTextField tfBookPages = new JTextField();
tfBookPages.setSize(140, 20);
tfBookPages.setLocation(420, 400);
menuFrame.add(tfBookPages);
menuFrame.setVisible(true);
}
,這是驗證部分
public void EntryCheck() {
if (tfBookName.getText().equals("")) {
EntryChecker++;
System.out.println(EntryChecker);
}
if (tfBookISBN.getText().equals("")) {
EntryChecker++;
}
if (tfBookCost.getText().equals("")) {
EntryChecker++;
}
if (tfBookPages.getText().equals("")) {
EntryChecker++;
}
if (tfBookPublishingHouse.getText().equals("")) {
EntryChecker++;
}
if (tfBookYearOfPublication.getText().equals("")) {
EntryChecker++;
}
if (tfBookCost.getText().equals("DD-MM-YYY")) {
EntryChecker++;
}
System.out.println(EntryChecker);
}
EntryChecker是一個整數,我每次當用戶離開文本字段沒有在裏面輸入信息時添加的值給它。
每當我調試程序時,我都會在此驗證行中遇到NullPointerException。
if (tfBookISBN.getText().equals("")) {
EntryChecker++;
任何人都有想法問題在哪裏,以及如何解決它?
謝謝!
你確定在創建該字段時,if語句中的'tfBookISBN'是否是相同的對象引用?我的意思是上面的代碼範圍是什麼,你有一個全球tfBookISBN?另一個內部的方法?即發佈更多的代碼:) – Yazan
@Yazan tfBookISBN是全球性和私人性的,我用一種方法聲明它,然後在另一個類中聲明它。 – Helvijs
@Yazan根據您的要求添加了兩種方法的所有代碼。 – Helvijs