ActionListener编译错误 - ;预期

时间:2013-12-23 20:53:29

标签: java swing compiler-errors

我正在尝试使用actionlistener从jbutton b1打开一个应用程序:

public void actionPerformed(ActionEvent ae)// outside the class
    {
        if(ae.getSource() == b1)//b1 is the button
        {
            try         
            {
                d.open(f);//f has the path to the exe file
            }
            catch(Exception ex) 
            {
                JOptionPane.showMessageDialog(OpenApps.this,ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

但是当我添加动作监听器时:

b1.add ActionListener(this);// inside the class

它出错了:

; expected in line b1.add ActionListener(this);

请帮忙。

3 个答案:

答案 0 :(得分:1)

确保b1.addActionListener(this);

之间没有任何空格

添加和ActionListener。

addActionListener是添加actionlistener的方法。

答案 1 :(得分:1)

基于

的代码示例
b1.add ActionListener(this);// inside the class

addActionListenr之间有空格。

b1.add ActionListener(this);// inside the class
      ^--- This won't help...

尝试,

b1.addActionListener(this);// inside the class

相反

答案 2 :(得分:1)

b1.add ActionListener(this);// inside the class

删除间距!

b1.addActionListener(this);// inside the class