覆盖Java中的GroupLayout构造函数

问题描述

为什么不能在类GroupLayout添加构造函数,如:

public class xxx extends GroupLayout {

  public xxx(Container host,String...arg) {
    //code
  }
}

解决方法

GroupLayout没有无参数的构造函数,这意味着Java不能对其构造函数进行隐式调用,因此您将得到一个编译时错误。您只需要调用super(host)作为方法的第一行,即可调用GroupLayout确实具有的构造函数。试试:

public class SubGroupLayout extends GroupLayout {

    public SubGroupLayout(Container host,String ...arg) {
        super(host);
        // then,do your own code here
    }
}

有关更多详细信息,请参见Using the Keyword supersuper() in constructor