gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:23:09 25 4
gpt4 key购买 nike

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

最佳答案

您可以检查用户是否已插入所有字段,如下所示:

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

关于java - 如何检查用户是否在 JDialogBox 中输入了某些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6329964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com