【发布时间】: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 线索,因为我认为此类设计中的一些可能是也有点矫枉过正......
【问题讨论】: