與此代碼的問題是,每當我運行此它顯示了編譯錯誤:對象被傳遞作爲參數未聲明
cannot find symbol:constructor mywindowadapter(frame1)
location:class mywindowadapter
mywindowadapter mwa=new mywindowadapter()"
import java.awt.*;
import java.awt.event.*;
/*<applet code=frame2 width=500 height=500>
</applet>*/
class frame2 extends Frame
{
frame2(String title)
{
super(title);
mywindowadapter mwa=new mywindowadapter();
addWindowListener(mwa);
}
public static void main(String ar[])
{
frame2 f=new frame2("my frame");
f.setVisible(true);
f.setSize(200,100);
}
public void paint(Graphics g)
{
g.drawString("hello frame",60,70);
}
}
class mywindowadapter extends WindowAdapter
{
mywindowadapter()
{
frame2 f=new frame2();
}
public void windowClosing(WindowEvent we)
{
f.setVisible(false);
System.exit(0);
}
}
下面的代碼是上述代碼的整流版本。我無法理解在前面的代碼中產生的錯誤。請幫忙!!
import java.awt.*;
import java.awt.event.*;
/*<applet code=frame2 width=500 height=500>
</applet>*/
class frame2 extends Frame
{
frame2(String title)
{
super(title);
mywindowadapter mwa=new mywindowadapter(this);
addWindowListener(mwa);
}
public static void main(String ar[])
{
frame2 f=new frame2("my frame");
f.setVisible(true);
f.setSize(200,100);
}
public void paint(Graphics g)
{
g.drawString("hello frame",60,70);
}
}
class mywindowadapter extends WindowAdapter
{
frame2 f;
mywindowadapter(frame2 f)
{
this.f=f;
}
public void windowClosing(WindowEvent we)
{
f.setVisible(false);
System.exit(0);
}
}
如果您收到此錯誤消息,則不會在編譯您顯示的代碼時發生。 – 2013-03-23 07:46:22
其編譯錯誤時顯示,我已檢查。 – 2013-03-23 07:55:14
錯誤消息說:「mywindowadapter(frame1)」。在第一個代碼片段中,frame1的出現次數爲零。零類和零變量就是這樣命名的。您沒有編譯您認爲的代碼,或者該消息不是來自此代碼。 – 2013-03-23 08:17:34