【问题标题】:how to check if user has input something or not in JDialogBox?如何检查用户是否在 JDialogBox 中输入了某些内容?
【发布时间】:2011-06-13 11:44:58
【问题描述】:

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

【问题讨论】:

  • 为什么不对这些字段执行null检查,如果这三个字段中的任何一个为空,请再次显示对话框,指示所有字段都是必需的。
  • 谢谢我明白了...但是关闭操作呢

标签: java jdialog


【解决方案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());
    }
}

【讨论】:

  • @mre:至少,这是我对 OP 的理解
  • @Eng.Fouad,如何将窗口侦听器添加到JOptionPane 对话框?
  • 我想你没有得到我的问题我想将窗口侦听器添加到 JOptionPane 而不是在 JFrame 上
  • @sanau 已更新。我希望我能很好地理解你的问题。
【解决方案2】:

我建议使用不同的方法来划分每个数据。

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 2019-11-29
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多