2013-11-01 31 views
1

代碼:addActionListener不能添加到給定的類型?

import java.awt.*; 
import javax.swing.*; 
import java.rmi.*; 
import java.awt.event.*; 

public class ServiceBrowser{ 
    JPanel mainPanel; 
    JComboBox serviceList; 
    ServiceServer server; 

    public void buildGUI(){ 
     JFrame frame = new JFrame("RMI Browser"); 
     mainPanel = new JPanel(); 
     frame.getContentPane().add(BorderLayout.CENTER, mainPanel); 

     Object[] services = getServiceList(); 
     //rmi registry lookup, get stub, call getServiceList 

     serviceList = new JComboBox(services); 
     //add services(array of objects) to Jcombobox, 
     //knows how to make displayable strings out of 
     //each thing in array 

     frame.getContentPane().add(BorderLayout.NORTH, serviceList); 
     serviceList.addActionListener(new MyListListener()); 

     frame.setSize(500,500); 
     frame.setVisible(true); 
    }//end buildGUI 

    void loadService(Object serviceSelection){ 
     try{ 
      Service svc = server.getService(serviceSelection); 
      mainPanel.removeAll(); 
      mainPanel.add(svc.getGuiPanel()); 
      mainPanel.validate(); 
      mainPanel.repaint(); 
     } catch(Exception ex){ 
      ex.printStackTrace(); 
     }//add actual service to gui after user selects one 
     //(this method called by event listener on combobox) 
     //call getService() on remote server(stub), pass it 
     //same string in the list, server returns actual service(serialized) 
     //and call getGuiPanel on service and add result (jpanel) to browser mainpanel 
    } 

    Object[] getServiceList(){ 
     Object obj = null; 
     Object[] services = null; 
     try{ 
      obj = Naming.lookup("rmi://127.0.0.1/ServiceServer"); 
     } catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
     server = (ServiceServer) obj; 
     //cast the stub to remote interface so we can call getServiceList on it 

     try{ 
      services = server.getServiceList(); 

     }catch(Exception ex){ex.printStackTrace();} 
    } 

    class MyListListener implements ActionListneer{ 
     public void actionPerformed(ActionEvent ev){ 
      Object selection = serviceList.getSelectedItem(); 
      loadService(selection); 
      //user made selection from jcombobox list, take selection 
      //load appropriate service 
     } 
    } 

    public static void main(String[] args){ 
     new ServiceBrowser().buildGUI(); 
    } 
} 

錯誤:

row 25: addactionlistener in class JComboBox<e> cannot be applied to given type 
    serviceList.addActionListener(new MyListListener()); 
ServiceBrowser.java uses unchecked or unsafe operations 

我知道這意味着有一個集合的地方,沒有聲明其類型,但我不知道是什麼的問題。我認爲這與使用一組對象的serviceList有關係嗎? JComboBox構造函數說它需要一個對象列表,所以我不確定這個錯誤。

回答

4

小錯字:

class MyListListener implements ActionListeneer { 
              ^

應該

class MyListListener implements ActionListener { 
+0

我不得不看到它之前,重新閱讀你的答案几次。 1+ –

+0

謝謝我的所有錯誤都消失了,除了「使用不安全或未經檢查的操作」。任何想法什麼是解決方案 – gallly

+0

您需要參數化您的'JComboBox'聲明/實現,例如, 'JComboBox serviceList = new JComboBox <>(services);' – Reimeus

相關問題