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

时间:2011-06-13 11:44:59

标签: java jdialog

我创建了一个自定义对话框,需要3个字段

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 , , "

3 个答案:

答案 0 :(得分: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:

好的,我想我现在明白你的问题,试试这个:

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());
    }
}

答案 1 :(得分: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()) + "'.");
    }
}

这样,您可以确定缺少哪个字段。

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

答案 2 :(得分:1)

简短回答,检查字符串中是否还有任何内容在打印之前。

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