代码:
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构造函数说它需要一个对象列表,所以我不确定错误。
答案 0 :(得分:4)
小错字:
class MyListListener implements ActionListeneer {
^
应该是
class MyListListener implements ActionListener {