简体   繁体   English

为JTextField设置不同的inputVerifier

[英]Set different inputVerifier for JTextFields

Here i set InputVerifier to my text field ( tf1 ) : 在这里,我将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");
        }
    }
}
}

Now i want to set input verifier to my second text field ( tf2 ) , But my tf2 verifier condition is different than tf1 . 现在我想将输入验证器设置为第二个文本字段( tf2 ),但是我的tf2验证器条件与tf1不同。

For example my verify() of tf2 should be like: 例如我的tf2 verify()应该像:

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

And so on for more text fields. 以此类推,获取更多文本字段。

How to verify two or more text fields with different conditions with one extended class? 如何使用一个扩展类来验证具有不同条件的两个或多个文本字段?

How to verify two or more text fields with different conditions with one extended class? 如何使用一个扩展类来验证具有不同条件的两个或多个文本字段?

You need to identify JTextField with a name: 您需要使用名称标识JTextField:

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

And then use it like ths: 然后像这样使用它:

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

In addition I think you need to check this : 另外,我认为您需要检查以下内容

The purpose of this class is to help clients support smooth focus navigation through GUIs with text fields 此类的目的是帮助客户支持通过带有文本字段的GUI的平滑焦点导航

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM