当我尝试将ActionListener添加到按钮时,我不能,

时间:2013-04-12 22:09:48

标签: java swing jbutton actionlistener

当我尝试运行此代码时,出现此错误:

Multiple markers at this line
    - Line breakpoint:CastleDefenders [line: 45] - CastleDefenders()
    - The method addActionListener(ActionListener) in the type AbstractButton is not applicable 
     for the arguments (CastleDefenders)

代码:

public CastleDefenders(){
    button = new JButton("Go!");
    lvl1.setToolTipText("Play the game on Easy");
    lvl2.setToolTipText("Play the game on Normal");
    lvl3.setToolTipText("Play the game on Hard");
    lvl1.setActionCommand("Easy");
    lvl2.setActionCommand("Normal");
    lvl3.setActionCommand("Hard");
    lvls.add(lvl1);
    lvls.add(lvl2);
    lvls.add(lvl3);
    pnl2.add(lvl1);
    pnl2.add(lvl2);
    pnl2.add(lvl3);
    button.addActionListener(this);
    tlkt = Toolkit.getDefaultToolkit();
    timer = new Timer();
    pnl2.setBounds(50, 5, 1, 10);
    btn.setBounds(220, 240, 5, 1);
    f.setJMenuBar(mb2);
    g.setJMenuBar(mb2);
    mnuFile.add(mnuItemQuit);
    mnuHelp.add(mnuItemAbout);
    mb.add(mnuFile);
    mb.add(mnuHelp);
    mb2.add(mnuFile);
    mb2.add(mnuHelp);
    pnl.add(btn);
    pnl2.add(lbl);
    pnl2.add(button);
    lbl.setBounds(0, 240, 10, 2);
    lbl.setEditable(false);
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(pnl, BorderLayout.CENTER);       
    f.addWindowListener(new ListenCloseWdw());
    g.getContentPane().setLayout(new BorderLayout());
    g.getContentPane().add(pnl2);   
    g.addWindowListener(new ListenCloseWdw());
    mnuItemAbout.addActionListener(new GetAboutInfo());     
    btn.addActionListener(new GetButtonClick());    
    mnuItemQuit.addActionListener(new ListenMenuQuit());
    add(button);


}

2 个答案:

答案 0 :(得分:0)

如果您希望它监听动作事件,则需要CastleDefenders类来实现ActionListener接口。从错误消息中可以看出您还没有这样做。

答案 1 :(得分:0)

在线: button.addActionListener(本); CastleDefenders的类型是什么? CastleDefenders是否实现了ActionListener?