简体   繁体   English

在另一个类中使用抽象类inputverifier java

[英]use abstract class inputverifier java in another class

I have two classes 我有两节课

public class Finestra extends javax.swing.JFrame{
    ........
    jtextField.setinputVerifier(.....):
}

public abstract class Verifica extends InputVerifier{
    String message;
    public Verifica(String message){
    }

    public abstract boolean verifica(JtextField c);

    public boolean verify(Jcomponent c){
         JTextField c = (JTextField) jc;
    if (esito(c) == false){
        return false;
    }else{
        return true;
    }
}
    }
}

I want to use Verifca class in finestra. 我想在finestra中使用Verifca类。 I don't extends it because there is javax.swing.JFrame. 我没有扩展它,因为有javax.swing.JFrame。 Can i do to use Verifica? 我可以使用Verifica吗? and is it a problem the only abstract method verifica. 并且这是一个问题唯一的抽象方法verifica。

I try this, but doesn' work 我试试这个,但没有用

      testo.setInputVerifier(new Verifica("error") {
         public boolean verifica(testo){
             if (testo.getText == null){
                return true;
             }else{
                  return false;
             }

           }

      });

What you're trying to do is to have an abstract class Verifica and then, when using it, to create an anonymous class with a custom implementation of the method verifica . 你要做的是拥有一个抽象类Verifica ,然后在使用它时,创建一个带有方法verifica的自定义实现的匿名类

In the anonymous class definition that you're doing, you must override the variable with the same signature like this for example: 在您正在进行的匿名类定义中,您必须使用相同的签名覆盖变量,例如:

testo.setInputVerifier(new Verifica("error") {
     @Override
     public boolean verifica(JTextField c) {
         if (testo.getText == null){
            return true;
         } else{
              return false;
         }
     }

  });

You cannot "bind" your variable testo as the parameter for your verifica method in the anonymous class. 您不能将变量testo “绑定”为匿名类中的verifica方法的参数。 You can, however, refer to testo directly from the outside scope of you anonymous class (so it gets bound in the implementation, not the signature of the method), resulting in something like this: 但是,您可以直接从您的匿名类的外部范围引用testo (因此它在实现中绑定,而不是方法的签名),从而产生如下所示:

public abstract class Verifica extends InputVerifier {
    public abstract boolean verifica();
}

and

testo.setInputVerifier(new Verifica("error") {
     public boolean verifica() {
         if (testo.getText == null) {
            return true;
         } else {
              return false;
         }
       }
  });

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

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