我正在尝试使用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);
请帮忙。
答案 0 :(得分:1)
确保b1.addActionListener(this);
之间没有任何空格添加和ActionListener。
addActionListener是添加actionlistener的方法。
答案 1 :(得分:1)
基于
的代码示例b1.add ActionListener(this);// inside the class
add
和ActionListenr
之间有空格。
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