【问题标题】:An abstract input verifier and the JDialog class抽象输入验证器和 JDialog 类
【发布时间】:2017-05-02 16:05:02
【问题描述】:

我试图了解我遇到的一个类 here 来验证我拥有的几个 JTextArea 上的输入。我的工作正常,但我对某些逻辑感到困惑,特别提到了使用 JDialog 作为父组件,希望有人能帮助我更好地理解它。

抽象验证器类:

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;

public abstract class AbstractValidator extends InputVerifier implements KeyListener{

private JDialog popup;
private Object parent;
private JLabel messageLabel;
private JLabel image;
private Point point;
private Dimension cDim;
private Color color;

private AbstractValidator(){
    color = new Color(243,255,159);
}

private AbstractValidator(JComponent c, String msg){
    this();
    c.addKeyListener(this);
    messageLabel = new JLabel(msg + " ");
    //image = new JLabel();         
}

public AbstractValidator(JDialog parent, JComponent c, String msg){
    this(c,msg);
    this.parent = parent;
    popup = new JDialog(parent);
    initComponents();
}

//implement the actual validation logic, returning false if the data is 
//invalid and true if it is not. you can also set the popup msg text with
//setMessage() before returning.
//param c is the component to be validated

protected abstract boolean validationCriteria(JComponent c);

//this method is called by Java when a component needs to be validated. 
//it should not be called directly. Don't override this unless you want to change
//validation behaviour. implement validationCriteria() instead.

@Override
public boolean verify(JComponent c){
    if(!validationCriteria(c)){
        if(parent instanceof Validatable)
            ((Validatable)parent).validateFailed();

        c.setBorder(BorderFactory.createLineBorder(Color.RED));
        popup.setSize(0,0);
        popup.setLocationRelativeTo(c);
        point = popup.getLocation();
        cDim = c.getSize();
        popup.setLocation(point.x-(int)cDim.getWidth()/2, point.y+(int)cDim.getHeight()/2);
        popup.pack();
        popup.setVisible(true);
        return false;
    }
    c.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    if(parent instanceof Validatable)
        ((Validatable)parent).validatePassed();
    return true;
}

protected void setMessage(String msg){
    messageLabel.setText(msg);   
}

@Override
public void keyPressed(KeyEvent e){
    popup.setVisible(false);
}

@Override
public void keyTyped(KeyEvent e){
}
@Override
public void keyReleased(KeyEvent e){}

private void initComponents(){
    popup.getContentPane().setLayout(new FlowLayout());
    popup.setUndecorated(true);
    popup.getContentPane().setBackground(color);
    popup.getContentPane().add(messageLabel);
    popup.setFocusableWindowState(false);

}
}

具体类:

package gui;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JTextField;

public class NotEmptyValidator extends AbstractValidator{
    public NotEmptyValidator(JDialog parent, JTextField c, String msg){
        super(parent,c, msg);
    }

    protected boolean validationCriteria(JComponent c){
        if(((JTextField)c).getText().equals("")){
            return false;
        }
        return true;
    }
}

(未使用的)接口:

package gui;

public interface Validatable {
    void validateFailed();
    void validatePassed();
}

在主 JPanel 中的 JTextField 上使用具体类:

textField.setInputVerifier(new NotEmptyValidator(new JDialog(), textField, "msg"));    

首先(但不重要)我很困惑为什么作者将抽象类中的构造函数分开?不过我认为这并不重要。

如前所述,我对 JDialog 思想的使用感到更加困惑。 从上面链接中的描述以及使用instanceof然后调用接口方法的代码来看,它看起来像是在首先调用verify()时(通过java)然后是实现的方法'validationCriteria( )'。

但令我困惑的是它是一个 JDialog,为什么它不只是一个 JComponent?我想我在这里遗漏了一些基础知识——这就是为什么我对具体类的实例化只使用“新”JDialog——尽管我将阅读 inputverifier 上的 oracle 线索,因为我认为此类设计中的一些可能是也有点矫枉过正......

【问题讨论】:

    标签: java swing


    【解决方案1】:

    如前所述,我对 JDialog 思想的使用感到更加困惑。从上面链接中的描述以及使用instanceof然后调用接口方法的代码来看,它看起来像是在首先调用verify()时(通过java)然后是实现的方法'validationCriteria( )'。

    他们似乎使用JDialog(或JFrame)作为他们自己内部JDialog 的参考点,用于在验证失败时显示消息。这是JDialog 的(可选)要求,允许将其放置在调用它的父窗口上

    根据示例代码,我想说他们只是作弊,虽然我没有测试过,但您可以通过 null(但您必须将其转换为 JDialog 或 JFrame )

    也可能有点旧(或者他们不知道一些额外的 API 功能),因为他们可以使用 setLocationRelativeTo 而不是手动定位对话框

    进入本质,validationCriteria 是您为组件提供验证的地方,正如您所描述的,它由verify 调用。这提供了作者可以控制验证过程并实施其定制的方法。请记住,c 是对您要验证的JComponent 的引用(即JTextArea)

    作者以WantsValidationStatus 的形式提供了额外的自定义,可以通过parent(JDialog 或JFrame)实现,它提供了一种关于验证过程状态的通知方式

    首先(但不重要)我很困惑为什么作者将抽象类中的构造函数分开?不过我认为这并不重要。

    我的猜测是,他们希望提供灵活的安排,但不了解 Swing API 提供的某些功能,例如。您可以将构造函数的数量减少到 1 并使用SwingUtilities#windowForComponent 来获取对包含该组件的窗口的引用

    但令我困惑的是它是一个 JDialog,为什么它不只是一个 JComponent?我想我在这里遗漏了一些基础知识——这就是为什么我对具体类的实例化只使用“新”JDialog——尽管我将阅读 inputverifier 上的 oracle 线索,因为我认为此类设计中的一些可能是也有点矫枉过正......

    嗯,其他两个 cmets 现在应该已经回答了这个问题:P

    离别的想法

    在我看来,这有点不对。我不会通过构造函数将任何组件传递给类,显示消息或采取其他操作不是责任,它的工作只是验证组件并返回true/false(然后控制焦点横向)。

    在我看来,InputVerifier 最好支持观察者模式,然后生成 validationFailed/validationPassed(或类似的)事件并让其他代表决定应该做什么完成了,但就是这样;)

    【讨论】:

    • 感谢您的深入回答。是的,我的主要困惑是关于您描述的验证过程的通知,而不是 JDialog 验证失败消息。对不起,我错过了弹出变量也是一个 JDialog 所以不是特别清楚。我同意我不确定这种方法是否理想,并且他正在尝试创建一个灵活/可重用的类和过于复杂的东西。所以我要去研究一下InputVerifier,看看我能不能想出类似于你描述的东西。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2011-06-09
    • 2014-02-12
    • 2020-10-03
    相关资源
    最近更新 更多