[英]Constructor call must be the first statement in a constructor in super()
I'm following a guide on how to create a working interface in Java and I've done the code according to the guide yet I get an error saying Constructor call must be the first statement in a constructor
, even though it is the first statement.我正在遵循有关如何在 Java 中创建工作接口的指南,我已经根据指南完成了代码,但我收到一条错误消息,说
Constructor call must be the first statement in a constructor
,即使它是第一条语句. I tried multiple solutions so I'm quite lost as none of them worked.我尝试了多种解决方案,所以我很迷茫,因为它们都不起作用。
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame {
private JLabel item1;
private void test () {
super ("Title 1"); //error happens here
}
}
You didn't create a constructor - constructors don't have return type of void.您没有创建构造函数 - 构造函数没有 void 返回类型。 It should be
它应该是
private test (){
super ("Title 1");
}
but you actually should stick to Java naming conventions and rename your class to Test
.但您实际上应该坚持 Java 命名约定并将您的类重命名为
Test
。
The constructor should not have a return value构造函数不应有返回值
private test () {
super(); // This should do
}
You need to remove the void
from your test
function.您需要从
test
函数中删除void
。
Your code must be like:你的代码必须是这样的:
public test (){
super ("Frame Title");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.