如何检查用户是否在JDialogBox中输入了某些内容?

如何检查用户是否在JDialogBox中输入了某些内容?

问题描述:

我创建了需要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://*.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

用户关闭对话框,您可以检查用户是否已经插入所有字段或不如下:

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

我建议分隔每个数据的不同方法。

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'. 

简短的回答,请检查是否有任何留在印刷前的字符串。

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