如何创建特定的类构造函数
我有一个非常简单的 Java JTable 问题。 我创建这个类是为了让我的应用程序的其余部分变得更容易。我在运行时收到错误。我知道这些错误意味着什么,但不知道还能尝试什么。
您将在代码中看到我想要完成的任务:
我的班级:
import javax.swing.*;
public class CPTable extends JScrollPane
{
private JTable table;
CPTable(Object [] headers, Object [][] data)
{
table = new JTable(data, headers);
this = new JScrollPane(table);//The line I can't figure out.
}
}
我的错误:(一个明显的错误)
cannot assign a value to final variable this
this = new JScrollPane(table);
和
incompatible types
found : javax.swing.JScrollPane
required: CPTable
I have a very simple Java JTable question.
I am creating this class to make things easier in the rest of my application. I receive an error when running it. I know what the errors mean, but have no idea what else to try.
You'll see in the code what I am trying to accomplish:
My Class:
import javax.swing.*;
public class CPTable extends JScrollPane
{
private JTable table;
CPTable(Object [] headers, Object [][] data)
{
table = new JTable(data, headers);
this = new JScrollPane(table);//The line I can't figure out.
}
}
My errors: (an obvious one)
cannot assign a value to final variable this
this = new JScrollPane(table);
and
incompatible types
found : javax.swing.JScrollPane
required: CPTable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
您不能重新分配
this
,但您可以使用super
(它必须是构造函数中的第一个语句)调用正确的超类构造函数。this
语句称为“构造函数委托”——它链接到另一个构造函数,以便您可以将表传递给超类并将其分配给您的表
领域。Try this:
You cannot reassign
this
, but you can cause the correct superclass constructor to be called by usingsuper
(which must be the first statement in your constructor).The
this
statement is called "constructor delegation"---it chains through to the other constructor so that you can pass the table to the superclass as well as assign it to yourtable
field.