Java无法找到符号

时间:2011-02-18 05:36:06

标签: java object compiler-construction applet

这是我的错误消息

test.java:17: cannot find symbol
symbol  : class MouseAdapter
location: class test
        private class click extends MouseAdapter
                                    ^
test.java:19: cannot find symbol
symbol  : class MouseEvent
location: class test.click
                public void mouseEntered(MouseEvent e)
                                         ^
test.java:14: cannot find symbol
symbol  : variable trayicon
location: class test
        trayicon.addMouseListener(new click());
        ^
3 errors

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class test extends JFrame
{

    private JFrame frame;

    public void init()
    {
        trayicon.addMouseListener(new click());
    }

        private class click extends MouseAdapter
        {
            public void mouseEntered(MouseEvent e)
            {
                {
                frame.setVisible(true);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

你需要:

import java.awt.event.*;

修改

你需要声明trayicon:

JLabel trayicon = new JLabel();

答案 1 :(得分:0)

添加到mellamokb,您使用的是变量 trayicon ,但未在任何地方声明。

所以添加

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

并声明 trayicon

答案 2 :(得分:0)

重现错误:

$cat >test.java <<. 
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class test extends JFrame
{

    private JFrame frame;

    public void init()
    {
        trayicon.addMouseListener(new click());
    }

        private class click extends MouseAdapter
        {
            public void mouseEntered(MouseEvent e)
            {
                {
                frame.setVisible(true);
            }
        }
    }
}
.
$javac test.java 
test.java:15: cannot find symbol
symbol  : class MouseAdapter
location: class test
        private class click extends MouseAdapter
                                    ^
test.java:17: cannot find symbol
symbol  : class MouseEvent
location: class test.click
            public void mouseEntered(MouseEvent e)
                                     ^
test.java:12: cannot find symbol
symbol  : variable trayicon
location: class test
        trayicon.addMouseListener(new click());
        ^
3 errors

添加导入

$ed test.java 
431
1i
import java.awt.event.*;
.
wq
456
$javac test.java 
test.java:13: cannot find symbol
symbol  : variable trayicon
location: class test
        trayicon.addMouseListener(new click());
        ^
1 error

在第13行声明trayicon

$ed test.java 
456
13i
        JLabel trayicon = new JLabel();// or something that makes sense
.
wq
528
$javac test.java 
$

固定!

结果代码:

$cat test.java 
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class test extends JFrame
{

    private JFrame frame;

    public void init()
    {
        JLabel trayicon = new JLabel();// or something that makes sense
        trayicon.addMouseListener(new click());
    }

        private class click extends MouseAdapter
        {
            public void mouseEntered(MouseEvent e)
            {
                {
                frame.setVisible(true);
            }
        }
    }
}

顺便说一下,你不需要第26和21行:

$ed test.java 
633
26d
21d
wq
605
$javac test.java 
$
$cat test.java 
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class test extends JFrame
{

    private JFrame frame;

    public void init()
    {
        JLabel trayicon = new JLabel();// or something that makes sense
        trayicon.addMouseListener(new click());
    }

        private class click extends MouseAdapter
        {
            public void mouseEntered(MouseEvent e)
            {
                frame.setVisible(true);
            }
        }
    }