2013-12-23 70 views
-3

我試圖用的ActionListener打開從一個JButton B1的應用程序:ActionListener編譯錯誤 - ;預計

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); 

請幫忙。

+1

你能告訴我們全班同學嗎?你記得'實現ActionListener'嗎? – Vallentin

回答

1

確保你沒有b1.addActionListener(this)之間的空間。

add和ActionListener。

addActionListener是添加actionlistener的方法。

+0

它通過刪除間距感謝.. – user3130713

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 

相反

+0

它的工作通過刪除間距謝謝.. – user3130713

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

刪除的間距!

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