Java 如何检查用户是否在JDialogBox中输入了内容?
我创建了一个需要3个字段的自定义对话框Java 如何检查用户是否在JDialogBox中输入了内容?,java,jdialog,Java,Jdialog,我创建了一个需要3个字段的自定义对话框 public class Test{ public static void main(String args[]) { JTextField firstName = new JTextField(); JTextField lastName = new JTextField(); JPasswordField password = new JPasswordField();
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());
}
});
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());
}
}
我建议采用不同的方法来划定每个基准面
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'.
简短回答,在打印前检查字符串中是否有剩余内容
if(firstName.getText().equals("")) {
//Respond appropriately to empty string
} else {
// Respond appropriately to non-empty string
}
为什么不对这些字段执行
null
检查,如果这3个字段中的任何一个为null,则再次显示对话框,表明所有字段都是必需的。请参阅:谢谢,我知道了。。。但是close呢operation@mre:至少,这是我从表格中了解到的OP@Eng.Fouad,如何将窗口侦听器添加到JOptionPane
对话框?我想你没有明白我的问题,我想将窗口侦听器添加到JOptionPane而不是打开JFrame@saanu更新。我希望我能很好地理解你的问题。