我的问题
在NetBeans for Java中编译此类。我试图简单地向每个JPanel添加一个ActionListener。然而,当我在代码中键入:为什么我的代码不能识别addActionListener(ActionListener e)方法?
`addActionListener(new SquareMouseListener);`
我得到的错误:
Cannot Find Symbol;
method addActionListener(MinePanel.SquareMouseListener)
location: class MinePanel
下面是完整的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.JPanel;
public class MinePanel extends JPanel{
final private int xPos, yPos;
final private int numXPanels, numYPanels;
final private boolean isBomb;
private MineFrame holderFrame;
private boolean seen;
public MinePanel(int xPos, int yPos, int numXPanels, int numYPanels, MineFrame holderFrame)
{
this.xPos = xPos;
this.yPos = yPos;
this.numXPanels = numXPanels;
this.numYPanels = numYPanels;
if(Math.random()<.1)
{
isBomb = true;
}
else isBomb = false;
seen = false;
this.holderFrame = holderFrame;
addActionListener(new SquareMouseListener());
}
@Override
public void paint(Graphics g)
{
//Color thisColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.LIGHT_GRAY);
g.fillRect(1,1,getWidth()-2,getHeight()-2);
}
private class SquareMouseListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent ae)
{
System.out.println("Action Performed");
}
}
}
我能做些什么? Netbeans的告诉我导入:
import static com.sun.java.accessibility.util.AWTEventMonitor.addActionListener;
,但我知道那是不对的,因为我尝试过了,没有工作,因为addActionListener方法应包括的java.awt.event。*;上面的导入。
在此先感谢!
因为MinePanel或JPanel没有定义它 –