import java.awt.*;
import java.awt.event.*;
public class MyFrame2 extends Frame{
private int x=200,y=200;
private int w=200,h=200;
Panel p1=new Panel();
public MyFrame2(String n){
this(n,x,y,w,h);  //看这里,怎么错了啊,错在哪里,谢谢指教
}
public MyFrame2(String s,int x,int y,int w,int h){
super(s);
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.setLayout(null);
p1.setBackground(Color.yellow);
p1.setBounds(w/4,h/4,w/2,h/2);
         }
}

解决方案 »

  1.   

    不能在调用父类型构造函数之前引用 x,y,等变量
    public MyFrame2(String n){
    this(n,x,y,w,h);  //看这里,怎么错了啊,错在哪里,谢谢指教
    }
    这段你该成如下
    public MyFrame2(String n){
    super(n);  //
    }
      

  2.   

    Cannot refer to an instance field x while explicitly invoking a constructor!
    y,w,h的情况和这个一样了。
      

  3.   

    你这个程序写出来一点目的都没有,你既然已经初始化了x,y,w,h就没必要再建一个构造函数MyFrame2(String n)了,把这个去掉,构造函数内是不能引用本类的构造函数,出来调用超类。如果要修改这些值,你只需建一个方法,用来修改或者读取。
      

  4.   

    首先我不管你想做什么.
    1请不要这样写代码!
    private int x=200,y=200;
    private int w=200,h=200;
    2关于this.
    例子
    class Employee{

    private String name;
    private double salary;
    private int id = Employee.assignId();
    private static int nextId = 1;

    Employee(){
    } protected Employee(String name, double salary){
    this.name = name;
    this.salary = salary;
    } private static int assignId(){
    int r = Employee.nextId;
    Employee.nextId++;
    return r;
    } protected Employee(double dbl){
    this("Employee #" + nextId, dbl);
    }

    public String toString(){
    return "姓名:" + this.name
    + "\nId号:" + this.id
    + "\n工资:" + this.salary;
    }
    }public class CallByConstructor{ public static void main(String[] args){

    Employee manager = new Employee();
    Employee staff = new Employee("李四", 3020);

    System.out.println(manager + "\n");
    System.out.println(staff + "\n");

    Employee[] temp = new Employee[3];
    temp[0] = new Employee(1820);
    temp[1] = new Employee(1200);
    temp[2] = new Employee(1500); for (Employee e : temp)
    System.out.println(e + "\n");
    }}
    如果你是想知道关于this可以调用另一个constructor,我想我上面的例子更适合你.