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);
}
}
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);
}
}
public MyFrame2(String n){
this(n,x,y,w,h); //看这里,怎么错了啊,错在哪里,谢谢指教
}
这段你该成如下
public MyFrame2(String n){
super(n); //
}
y,w,h的情况和这个一样了。
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,我想我上面的例子更适合你.