java - 为 JTextFields 设置不同的 inputVerifier

标签 java swing validation jtextfield inputverifier

这里我将 InputVerifier 设置为我的文本字段 (tf1):

public class MyInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText().trim();
    if (text.isEmpty()) return false;
    if (text.matches(".*\\d.*")) return false;

    return true;
}

public static class Tester extends JFrame implements ActionListener {
    JTextField tf1,tf2;
    JButton okBtn;

    public Tester() {
        add(panel(), BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Tester();
            }
        });
    }

    public JPanel panel() {
        JPanel panel = new JPanel();
        okBtn = new JButton("Ok");
        okBtn.addActionListener(this);
        tf1 = new JTextField(10);
        tf2 = new JTextField(10);
        tf1.setInputVerifier(new MyInputVerifier());
        tf2.setInputVerifier(new MyInputVerifier());
        panel.add(tf1);
        panel.add(tf2);
        panel.add(okBtn);
        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MyInputVerifier inputVerifier = new MyInputVerifier();
        if (e.getSource() == okBtn) {
            if (inputVerifier.verify(tf1)){
                JOptionPane.showMessageDialog(null, "True Value");
            }
            else JOptionPane.showMessageDialog(null, "False Value");
        }
    }
}
}

现在我想将输入 validator 设置为我的第二个文本字段 (tf2),但我的 tf2 validator 条件与 tf1 不同。

例如,我的 tf2verify() 应该是这样的:

@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText().trim();
    if (text.equalsIgnoreCase("Me")) return true;
    return true;
}

等等更多文本字段。

如何使用一个扩展类验证具有不同条件的两个或多个文本字段?

最佳答案

如何使用一个扩展类验证两个或多个具有不同条件的文本字段?

您需要使用名称来标识 JTextField:

JTextField jTextField = new JTextField();
jTextField.setName("tf1");

然后像这样使用它:

   public boolean verify(JComponent input) {
        String name = input.getName();

        if(name.equals("tf1"))
        {
            String text = ((JTextField) input).getText().trim();
            if (text.isEmpty()) return false;
            if (text.matches(".*\\d.*")) return false;
        }
        if(name.equals("tf2"))
        {
            //other condition
            return true;
        }

        return true;
    }

另外我认为你需要检查this :

The purpose of this class is to help clients support smooth focus navigation through GUIs with text fields

关于java - 为 JTextFields 设置不同的 inputVerifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19099383/

相关文章:

java - JTable焦点查询?

java - 如何从按钮关闭 jdialog?

java - 高效的网络摄像头库

java - 卡在 Thread 和 updateGraphics 上

javascript - jquery验证后的ajax请求

java - 从 Java 备份 MySQL 时出错

java - 由字符串数组填充的 JComboBox(使用 for 循环)未出现

java - Swing:如何从 JTable 中拖动多行?

oop - 如何验证给定域的类图?

java - 如何在 Xerces 中使用语法验证文档