gpt4 book ai didi

java - 多个 JTextfield 的空字符串验证

转载 作者:可可西里 更新时间:2023-11-01 06:55:50 26 4
gpt4 key购买 nike

有没有一种方法可以在没有 if else 结构的情况下验证 java 中的多个 JTextfield。我有一组 13 个字段,当没有为 13 个字段中的任何一个输入时我想要一条错误消息,并且能够将焦点设置到该特定文本框。这是为了防止用户将空数据输入数据库。有人可以告诉我如何在没有像下面这样的 if else 结构的情况下实现这一点。

if (firstName.equals("")) {
JOptionPane.showMessageDialog(null, "No data entered");
} else if (lastName.equals("")) {
JOptionPane.showMessageDialog(null, "No data entered");
} else if (emailAddress.equals("")) {
JOptionPane.showMessageDialog(null, "No data entered");
} else if (phone.equals("")) {
JOptionPane.showMessageDialog(null, "No data entered");
} else {
//code to enter values into MySql database

以上代码来自提交注册按钮的 actionperformed 方法。尽管将 MySQL 中的字段设置为 NOT NULL,但从 Java GUI 中接受了空字符串。为什么是这样?我希望可能会抛出一个空字符串异常,我可以从中自定义验证消息,但由于接受了空字段而无法这样做。

谢谢

最佳答案

只是为了好玩,动动小手指演示了一个可重复使用的验证设置,它确实使用了核心 Swing 中可用的功能。

合作者:

  • 包含验证逻辑的 InputVerifier。这里只是检查验证字段中的空文本。注意
    • 验证一定不能有副作用
    • shouldYieldFocus 被覆盖以不限制焦点遍历
    • 所有文本字段都是同一个实例
  • 一个提交操作,它通过显式调用 inputVerifier(如果有的话)来检查其父级的所有子级的有效性,如果任何无效则什么都不做
  • 一种非常简单但普遍可用的错误消息机制,采用输入字段的标签

一些代码片段

// a reusable, shareable input verifier
InputVerifier iv = new InputVerifier() {

@Override
public boolean verify(JComponent input) {
if (!(input instanceof JTextField)) return true;
return isValidText((JTextField) input);
}

protected boolean isValidText(JTextField field) {
return field.getText() != null &&
!field.getText().trim().isEmpty();
}

/**
* Implemented to unconditionally return true: focus traversal
* should never be restricted.
*/
@Override
public boolean shouldYieldFocus(JComponent input) {
return true;
}

};
// using MigLayout for lazyness ;-)
final JComponent form = new JPanel(new MigLayout("wrap 2", "[align right][]"));
for (int i = 0; i < 5; i++) {
// instantiate the input fields with inputVerifier
JTextField field = new JTextField(20);
field.setInputVerifier(iv);
// set label per field
JLabel label = new JLabel("input " + i);
label.setLabelFor(field);
form.add(label);
form.add(field);
}

Action validateForm = new AbstractAction("Commit") {

@Override
public void actionPerformed(ActionEvent e) {
Component source = (Component) e.getSource();
if (!validateInputs(source.getParent())) {
// some input invalid, do nothing
return;
}
System.out.println("all valid - do stuff");
}

protected boolean validateInputs(Container form) {
for (int i = 0; i < form.getComponentCount(); i++) {
JComponent child = (JComponent) form.getComponent(i);
if (!isValid(child)) {
String text = getLabelText(child);
JOptionPane.showMessageDialog(form, "error at" + text);
child.requestFocusInWindow();
return false;
}
}
return true;
}
/**
* Returns the text of the label which is associated with
* child.
*/
protected String getLabelText(JComponent child) {
JLabel labelFor = (JLabel) child.getClientProperty("labeledBy");
return labelFor != null ? labelFor.getText() : "";
}

private boolean isValid(JComponent child) {
if (child.getInputVerifier() != null) {
return child.getInputVerifier().verify(child);
}
return true;
}
};
// just for fun: MigLayout handles sequence of buttons
// automagically as per OS guidelines
form.add(new JButton("Cancel"), "tag cancel, span, split 2");
form.add(new JButton(validateForm), "tag ok");

关于java - 多个 JTextfield 的空字符串验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032757/

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