InputVerifier不显示每个组件图标(标签) [英] InputVerifier don't display each component icon(lable)

查看:78
本文介绍了InputVerifier不显示每个组件图标(标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为其设置输入验证程序的表单.

I have a form that set a input verifier to it.

我希望当用户为文本字段输入正确的值并想转到其他文本字段时,除了文本字段外还应显示一个复选图标. 但是现在在我的代码中,当用户在第一个文本字段上输入正确的值时,转到另一个位置,两个图标一起显示!

I want when a user type a correct value for a text field and want to go to other text field, a check icon should be display besides of text field. But now in my code, when user type a correct value on first text field an go to other, Two icons displayed together!

public class UserDialog extends JDialog {

JButton cancelBtn, okBtn;
JTextField fNameTf, lNameTf;
JRadioButton maleRb, femaleRb;
ButtonGroup group;
JLabel fNameLbl, fNamePicLbl, lNameLbl, lNamePicLbl, genderLbl, tempBtn, temp3;

    public UserDialog() {
    add(createForm(), BorderLayout.CENTER);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocation(400, 100);
    pack();
    setVisible(true);
}
    public JPanel createForm() {
    JPanel panel = new JPanel();
    ImageIcon image = new ImageIcon("Check.png");

    okBtn = new JButton("Ok");
    cancelBtn = new JButton("Cancel");
    tempBtn = new JLabel();
    fNameLbl = new JLabel("First Name");
    fNamePicLbl = new JLabel(image);
    fNamePicLbl.setVisible(false);
    lNameLbl = new JLabel("Last Name");
    lNamePicLbl = new JLabel(image);
    lNamePicLbl.setVisible(false);
    genderLbl = new JLabel("Gender");

    maleRb = new JRadioButton("Male");
    femaleRb = new JRadioButton("Female");
    temp3 = new JLabel();
    group = new ButtonGroup();
    group.add(maleRb);
    group.add(femaleRb);

    fNameTf = new JTextField(10);
    fNameTf.setName("FnTF");
    fNameTf.setInputVerifier(new MyVerifier(new JComponent[]{maleRb, femaleRb, okBtn}));
    lNameTf = new JTextField(10);
    lNameTf.setName("LnTF");
    lNameTf.setInputVerifier(new MyVerifier(new JComponent[]{maleRb, femaleRb, okBtn}));

    panel.add(fNameLbl);
    panel.add(fNameTf);
    panel.add(fNamePicLbl);
    panel.add(lNameLbl);
    panel.add(lNameTf);
    panel.add(lNamePicLbl);
    panel.add(genderLbl);
    JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    radioPanel.add(maleRb);
    radioPanel.add(femaleRb);
    panel.add(radioPanel);
    panel.add(temp3);
    panel.add(okBtn);
    panel.add(cancelBtn);
    panel.add(tempBtn);

    panel.setLayout(new SpringLayout());
    SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 10, 80, 60);
    return panel;
}
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new UserDialog();
        }
    });
}

public class MyVerifier extends InputVerifier {
    private JComponent[] component;

    public MyVerifier(JComponent[] components) {
        component = components;
    }

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

        if (name.equals("FnTF")) {
            String text = ((JTextField) input).getText().trim();
            if (text.matches(".*\\d.*") || text.length() == 0) {
                //disable dependent components
                for (JComponent r : component) {
                    r.setEnabled(false);
                }
                return false;
            }
        } else if (name.equals("LnTF")) {
            String text = ((JTextField) input).getText();
            if (text.matches(".*\\d.*") || text.length() == 0) {
                //disable dependent components
                for (JComponent r : component) {
                    r.setEnabled(false);
                }
                return false;
            }
        }
        //enable dependent components
        for (JComponent r : component) {
            r.setEnabled(true);
        }
        fNamePicLbl.setVisible(true);
        lNamePicLbl.setVisible(true);
        return true;
    }
}
}
}

已更新

 public class MyVerifier extends InputVerifier {
    private JComponent[] component;

    public MyVerifier(JComponent[] components) {
        component = components;
    }

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

        if (name.equals("FnTF")) {
            String text = ((JTextField) input).getText().trim();
            if (text.matches(".*\\d.*") || text.length() == 0) {
                return false;
            }
        } else if (name.equals("LnTF")) {
            String text = ((JTextField) input).getText();
            if (text.matches(".*\\d.*") || text.length() == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean shouldYieldFocus(JComponent input) {
        boolean isValidDate = verify(input);
        if (isValidDate) {
            for (JComponent r : component) {
                r.setEnabled(true);
            }
        } else {
            for (JComponent r : component) {
                r.setEnabled(false);
            }
        }
        return isValidDate;
    }

推荐答案

但是现在在我的代码中,当用户在第一个文本字段上输入正确的值时 转到另一个,两个图标一起显示!

But now in my code, when user type a correct value on first text field an go to other, Two icons displayed together!

因为您这样做了:(阅读评论)

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

        if (name.equals("FnTF")) {
            // your code
            }
        } else if (name.equals("LnTF")) {
            // your code

        }
        //enable dependent components
        for (JComponent r : component) {
            r.setEnabled(true);
        }
        /* And Now we are here */
        fNamePicLbl.setVisible(true); 
        lNamePicLbl.setVisible(true);
         // making visible two of them at once as soon as verify is called 
            // on any one of the components, verifier is registered

        return true;
    }

setVisible也应由if-else条件控制.为了更好地理解,您需要执行以下操作:

setVisible should be controlled by the if-else condition too. For your better understanding you need to do something like this:

      if (text.matches(".*\\d.*") || text.length() == 0) {
           // your code
        }
        else
        {
            fNamePicLbl.setVisible(true);

        } 

第二期:

    fNameTf.setInputVerifier(new MyVerifier(new JComponent[]{maleRb, femaleRb, okBtn}));
    lNameTf = new JTextField(10);
    lNameTf.setName("LnTF");
    lNameTf.setInputVerifier(new MyVerifier(new JComponent[]{maleRb, femaleRb, okBtn}));

MyVerfier具有用于验证两个输入字段的通用代码.但是,您正在使用相同的组件实例创建它的两个实例.创建一个并将其设置为两个字段的输入验证器.

The MyVerfier has the common code to verify both input field. But you are creating two instances of it with same instances of components. Create one and set it as the input verifier of the two field.

您可能想为两个文本字段创建两个不同的InputVerifier类. FnTFVerifierLnTFVerifier.然后输入与它们相关的验证码,例如,启用单选按钮并使用check.png显示标签.大多数if-else检查将消失.

You might want to create two different InputVerifier class for the two Text Field. FnTFVerifier and LnTFVerifier. Then put your verification code that relates them e.g., enabling the radio buttons and showing the label with check.png. most of the if-else checking will go-away.

但是我认为,这实际上不是最好的方法.由于两个文本字段具有通用功能,因此一个InputVerifier类和实例就足够了.您只需要将输入文本字段和相关的cehckLabel封装到一个组件,然后将InputVerifier实例注册到该组件.

But I think, this should not really be the preferable way. As the two text field has the common functionality, one InputVerifier class and instance is sufficient. you would have to just encapsulate the input text field and related cehckLabel to one component, then register the InputVerifier instances to this component.

第三个问题:您正在滥用验证功能:

验证功能只能用于验证数据:在用户要求的条件下,数据是否有效.它应该什么也不做. InputVerifier具有另一个功能 boolean ShouldYieldFocus(Jcomponent) :在将焦点转移到另一个请求它的Swing组件之前,将调用输入验证程序的shouldYieldFocus方法,该方法确定正在验证的组件应该失去焦点还是不是.仅当此方法返回true时,才会转移焦点.但是,您应该在此函数中编写所需的组件状态更改.

The verify function is meant to be used for nothing but verify data: whether data is valid or not with user required condition. It should do nothing more. InputVerifier has another function boolean ShouldYieldFocus(Jcomponent): Before focus is transfered to another Swing component that requests it, the input verifier's shouldYieldFocus method is called, which decides whither the component under verification should lose focus or not. Focus is transferred only if this method returns true. You should however write the required state change of components inside this function.

public boolean shouldYieldFocus(JComponent input) {
    boolean isDataValid =  verify(input);

    if(isDataValid); //do stuff

    return isDataValid; // if verify is true(valid) return true;
 }

这篇关于InputVerifier不显示每个组件图标(标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆