2011-06-13 66 views
2

我創建了需要3場如何檢查用戶是否在JDialogBox中輸入了某些內容?

public class Test{ 

    public static void main(String args[]) 

    { 

     JTextField firstName = new JTextField(); 

     JTextField lastName = new JTextField(); 

     JPasswordField password = new JPasswordField(); 

     final JComponent[] inputs = new JComponent[] { 

         new JLabel("First"), 

         firstName, 

         new JLabel("Last"), 

         lastName, 

         new JLabel("Password"), 

         password 

     }; 

     JOptionPane.showMessageDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 

     System.out.println("You entered " +firstName.getText() + ", " +lastName.getText() + ", " +password.getText()); 

    } 
} 

如何檢查,如果用戶插入的所有字段或不是自定義對話框?而且,即使用戶關閉對話框,它顯示

You entered , , 

我要檢查用戶輸入的字段和關閉應用程序,如果沒有dsplaying

"You entered , , " 
+0

爲什麼不對這些字段執行'null'檢查,如果這3個字段中的任何一個爲空,則再次顯示對話框,指示所有字段都是必需的。 – mre 2011-06-13 11:52:42

+0

請參閱:http://stackoverflow.com/questions/4608124/how-to-assign-a-sepecifc-action-to-the-cancel-button-within-joptionpane-show – 2011-06-13 11:53:04

+0

謝謝,我明白了......但是什麼關於關閉操作 – sanu 2011-06-13 11:54:24

回答

1

用戶關閉對話框,您可以檢查用戶是否已經插入所有字段或不如下:

if(firstName.getText() != "" && lastName.getText() != "" && password.getText() != "") 
    System.out.println("All fields have been filled!"); 
else 
    System.out.println("Some fields are need to be filled!"); 

編輯:

爲了關閉對話框後,顯示一個消息,你可以不喜歡它:

myframe.addWindowListener(new WindowAdapter() 
{ 
    public void windowClosing(WindowEvent e) 
    { 
     JOptionPane.showMessageDialog(null,"You entered " + firstName.getText() + ", " + lastName.getText() + ", " +password.getText()); 
    } 
}); 

EDIT2:

OK,我想我現在明白你的問題,試試這個:

public class Test 
{ 
    public static void main(String args[]) 
    { 
     JTextField firstName = new JTextField(); 
     JTextField lastName = new JTextField(); 
     JPasswordField password = new JPasswordField(); 
     final JComponent[] inputs = new JComponent[] 
     { 
      new JLabel("First"), 
      firstName, 
      new JLabel("Last"), 
      lastName, 
      new JLabel("Password"), 
      password   
     }; 
     int i = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 
     if(i == 0) System.out.println("You entered " + firstName.getText() + ", " + lastName.getText() + ", " + password.getText()); 
    } 
} 
+0

我不認爲這是OP正在尋找... – mre 2011-06-13 11:53:37

+0

@mre:至少,這是我理解的形式在OP – 2011-06-13 12:09:20

+0

@ Eng.Fouad,如何將一個窗口監聽器添加到' JOptionPane'對話框? – mre 2011-06-13 12:09:22

1

我建議分隔每個數據的不同方法。

import javax.swing.JComponent; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

public class Test { 

public static void main(String args[]) 
    { 
     JTextField firstName = new JTextField(); 
     JTextField lastName = new JTextField(); 
     JPasswordField password = new JPasswordField(); 
     final JComponent[] inputs = new JComponent[] { 
       new JLabel("First"), 
       firstName, 
       new JLabel("Last"), 
       lastName, 
       new JLabel("Password"), 
       password 
     }; 

     JOptionPane.showMessageDialog(null, inputs, "My custom dialog",JOptionPane.PLAIN_MESSAGE); 

     System.out.println("You entered '" + 
      firstName.getText() + "', '" + 
      lastName.getText() + "', '" + 
      //don't ignore deprecation warnings! 
      new String(password.getPassword()) + "'."); 
    } 
} 

這樣的話,你可以知道哪些字段(S)中缺少。

You entered '', 'johnson', ''. 
You entered 'john', '', ''. 
You entered '', '', 'johnsjohnson'. 
1

簡短的回答,請檢查是否有任何留在印刷前的字符串。

if(firstName.getText().equals("")) { 
    //Respond appropriately to empty string 
} else { 
    // Respond appropriately to non-empty string 
} 
相關問題