2013-03-22 91 views
-2

这段代码的问题是,每当我运行这个它显示了对象作为参数,为什么不能我们声明的类,而不是

编译错误 -

“找不到符号内通过mywindowadapter(帧1)

位置:类mywindowadapter mywindowadapter MWA =新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[]) 
    { 
     frame1 f=new frame1("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=frame1 width=500 height=500> 
</applet>*/ 
class frame1 extends Frame 
{ 
    frame1(String title) 
    { 
     super(title); 
     mywindowadapter mwa=new mywindowadapter(this); 
     addWindowListener(mwa);  
    } 
    public static void main(String ar[]) 
    { 
     frame1 f=new frame1("my frame"); 
     f.setVisible(true); 
     f.setSize(200,100); 

    } 
    public void paint(Graphics g) 
    { 
     g.drawString("hello frame",60,70); 
    } 
} 
class mywindowadapter extends WindowAdapter 
{ 
    frame1 f; 
    mywindowadapter(frame1 f) 
    { 
     this.f=f; 
    } 
    public void windowClosing(WindowEvent we) 
    { 
     f.setVisible(false); 
     System.exit(0); 
    } 
} 
+2

那么在你的代码中有一堆语法错误,这只是一个复制过去的问题?你能详细说明“为什么我们不能简单地在下面的类中声明对象,而不是将它作为参数传递。”给我错误的确切代码。 – 2013-03-22 19:09:07

+2

代码是显示你正在尝试做什么(并产生错误)在上面张贴?如果是这样,在哪里?另外,有什么错误?你粘贴的代码确实有几个语法错误。 – jedwards 2013-03-22 19:12:37

回答

0

因为在构造函数中frame1你想利用的Adapter pattern和代表处理事件的适配者frame1其中引用您作为参数传递给创建一个适配器实例。此适配器由您添加到frame1的侦听器列表中的侦听器使用。

+0

抱歉,请你用宽松的词语解释以上内容...... – 2013-03-23 07:44:08

相关问题