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

发布于 2024-11-14 23:02:24 字数 992 浏览 1 评论 0原文

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

I have created a custom dialog box which requires 3 fields

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

    }
}

How do I check if the user has inserted all fields or not? And even if user close the dialog box it display

You entered , , 

I want to check user input in field and close application if user close dialog box without dsplaying

"You entered , , "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-11-21 23:02: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());
    }
});

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

You can check if user has inserted all fields or not as follows:

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!");

EDIT:

For displaying a message after closing the dialog box, you can do it like:

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

EDIT2:

OK, I think I understand your question now, try this:

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());
    }
}
夏日浅笑〃 2024-11-21 23:02:24

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

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

I suggest a different approach of delimiting each datum.

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()) + "'.");
    }
}

That way, you can tell which field(s) is missing in.

You entered '', 'johnson', ''.
You entered 'john', '', ''.
You entered '', '', 'johnsjohnson'.
风尘浪孓 2024-11-21 23:02:24

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

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

Short answer, check to see if there is anything left in the String before print.

if(firstName.getText().equals("")) {
    //Respond appropriately to empty string
} else {
    // Respond appropriately to non-empty string
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文