why am I getting this error
Why Am I getting an error. In eclipse it says constructor call should be the first line. It is the first line. or you can not extend Main?
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends 开发者_如何学JAVAJFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
public Main(){
super("title bar");
}
}
}
Your Main
constructor should sit outside of the main
method. Like so:
public class Main extends JFrame {
public Main() {
super("title bar");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
}
}
The constructor should be outside public static void main(String[] args) {
It's a function and you can't have a constructor inside a function .
You're trying to define a constructor (public Main
) within a static method. That's not valid in Java.
You probably meant something more like this:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
}
// The constructor isn't *inside* `main` anymore:
public Main(){
super("title bar");
}
}
There are multiple errors.
The first error is that you're defining the constructor of the class in a method. This is illegal which results in the compiler complaining that it expected the new
keyword instead of public
.
Secondly, super
class methods have to be called in the first line. But since, the compiler is now confused due to the previous error, it has reported so.
You might also want to improve on the class naming convention. It is very easy to get confused between the main(String args[]
method, which is the entry point into your code, the Main
class, and its constructor Main()
(which would generated by the compiler).
精彩评论