Java 为什么我的代码不能识别addActionListener(ActionListener e)方法? 我的问题
在NetBeans for Java中编译此类。我试图简单地向每个JPanel添加一个ActionListener。但是,当我键入代码时:Java 为什么我的代码不能识别addActionListener(ActionListener e)方法? 我的问题,java,events,import,awt,actionlistener,Java,Events,Import,Awt,Actionlistener,在NetBeans for Java中编译此类。我试图简单地向每个JPanel添加一个ActionListener。但是,当我键入代码时: `addActionListener(new SquareMouseListener);` 我得到一个错误: Cannot Find Symbol; method addActionListener(MinePanel.SquareMouseListener) location: class MinePanel 以下是完整的代码: 但我
`addActionListener(new SquareMouseListener);`
我得到一个错误:
Cannot Find Symbol;
method addActionListener(MinePanel.SquareMouseListener)
location: class MinePanel
以下是完整的代码:
但我知道这是不对的,因为我试过了,但没有成功,因为addActionListener应该包含在java.awt.event.*中;请在上面输入
提前谢谢 只需键入
这个。添加并按ctrl+space。您将看到可以添加到JPanel的侦听器类型。
也许你需要鼠标听器
this.addMouseListener(新的YourListener())代码>
其中,YourListener
实现了MouseListener
接口。我发现了问题所在,我想发布一个答案,以防其他人看到
在尝试将ActionListener添加到JPanel时存在问题,这是无法完成的。JPanel没有预定义的addActionListener()方法,因为它是一个较低级别的组件。正如@Jigar Joshi所说,JPanel没有定义动作侦听器事件
谢谢大家的帮助 因为MinePanel或JPanel没有定义它
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");
}
}
}
import static com.sun.java.accessibility.util.AWTEventMonitor.addActionListener;