当前位置: 代码迷 >> Java相关 >> 郁闷,怎么找不到符号呢?
  详细解决方案

郁闷,怎么找不到符号呢?

热度:105   发布时间:2010-09-27 14:29:00.0
郁闷,怎么找不到符号呢?
import java.awt.*;
import java.awt.event.*;

public class TestDemo1
{
    public static void main(String[] args)
    {
        new Frame1();
    }
}


class Frame1 implements ActionListener
{
    static final int Height1=300;
    static final int Width1=500;
   
    Frame1()
    {
        Frame f1=new Frame("this is the first frame");
        f1.setSize(Width1,Height1);
        f1.setVisible(true);
        
        f1.setLayout(null);
        Button btn1=new Button("test");
        f1.add(btn1);
        btn1.setBounds(30,30,30,30);
        f1.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        
        btn1.addActionListener(this);   
    }
   
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==btn1)
        {
            new Frame2();
        }
        
    }
}

class Frame2
{
    static final int Height2=300;
    static final int Width2=500;
   
    Frame2()
    {
        Frame f2=new Frame("this is the second frame");
        f2.setSize(Width2,Height2);
        f2.setVisible(true);
        
        Button btn2=new Button("test");
        f2.add(btn2);
        
    }
}

编译报错:
--------------------Configuration: TestDemo1 - JDK version  <Default> - <Default>--------------------
E:\pro_files\JCreatorV4\MyProjects\TestDemo1\src\TestDemo1.java:41: 找不到符号
符号: 变量 btn1
位置: 类 Frame1
        if(e.getSource()==btn1)
                          ^
1 错误
搜索更多相关的解决方案: 符号  

----------------解决方案--------------------------------------------------------
楼主可以用eclipse啦
41行有错误
if(e.getSource()==btn1)
但是btn1未声明
注意你的btn1是在构造函数里声明的,其他方法里引用btn1是不行的
----------------解决方案--------------------------------------------------------
回复 2楼 shellingford
哦…知道了。
马上就要转Eclipse了,现在刚学Java,先用这个试试。呵呵…
谢谢!
----------------解决方案--------------------------------------------------------
  相关解决方案