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

问题描述 投票:0回答:3

我创建了一个需要 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 , , 

我想检查用户在字段中的输入,如果用户关闭对话框而不显示,则关闭应用程序

"You entered , , "
java swing jdialog
3个回答
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());
    }
});

编辑2:

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

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
投票

我建议采用不同的方法来界定每个数据。

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

1
投票

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

if(firstName.getText().equals("")) {
    //Respond appropriately to empty string
} else {
    // Respond appropriately to non-empty string
}
© www.soinside.com 2019 - 2024. All rights reserved.