ActionListener comboBoxL = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if("comboBoxChanged".equals(e.getActionCommand())) {
iconLabel.setIcon(icons[comboBox.getSelectedInde()]);
}
}
};
编译后,出现以下错误:
c:\java\work>javac LunarPhases.java
LunarPhases.java:52: 缺少返回语句
        }
        ^
1 错误
大侠帮我解决一下,谢谢了

解决方案 »

  1.   

    对啊,代码贴少了。BTW,if("comboBoxChanged".equals(e.getActionCommand())) 这样写看了很不习惯啊if(e.getActionCommand().equals("comboBoxChanged"))这样如何?呵呵
      

  2.   

    我全贴出来啊,我手写的一个JFC程序,因为没有通过编译,可能还有别的错误,大侠们帮我找下啊
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class LunarPhases extends JFrame {
      JPanel mainPane,selectPane,displayPane;
      JComboBox comboBox;
      ImageIcon[] icons = {new ImageIcon("c:/image0.jpg"),
         new ImageIcon("c:/image1.jpg"),
         new ImageIcon("c:/image2.jpg"),
         new ImageIcon("c:/image3.jpg"),
         new ImageIcon("c:/image4.jpg"),
         new ImageIcon("c:/image5.jpg"),
         new ImageIcon("c:/image6.jpg"),
         new ImageIcon("c:/image7.jpg"),
         new ImageIcon("c:/image8.jpg")
        };
      JLabel iconLabel ;
      String[] items = {"0",
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8"
     };
      public LunarPhases() {
        super("Lunar Phases");
      }
      public Component creatComponent() {
        mainPane = new JPanel(new GridLayout(2,1));//create and set the mainpane
        mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        mainPane.setSize(100,100);
        selectPane = new JPanel(new GridLayout(0,1));//create and set the selectpane
        selectPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createEmptyBorder(5,5,5,5),
    "Select Phase"));
        selectPane.setSize(100,50);
        comboBox = new JComboBox(items);//create and set the combobox and add it on  
        comboBox.addActionListener(comboBoxL);
        selectPane.add(comboBox);
        displayPane =new JPanel(new GridLayout(0,1));//create and set the displaypane
        displayPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createEmptyBorder(5,5,5,5),
    "Select Phase"));
        displayPane.setSize(100,100);
        iconLabel = new JLabel(icons[0]);//add the icon
        displayPane.add(iconLabel);
        mainPane.add(selectPane);
        mainPane.add(displayPane);
      }
      ActionListener comboBoxL = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          if("comboBoxChanged".equals(e.getActionCommand())) {
            iconLabel.setIcon(icons[comboBox.getSelectedIndex()]);
          }
        }
      };
      public void createAndShowGUI() {
        setDefaultLookAndFeelDecorated(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
      }
      public static void main(String args[]) {
        new LunarPhases().createAndShowGUI();
      }
    }





      

  3.   

    creatComponent没有返回值,当然有错,要么程序return一个component,要么改成void,看你的需要
      

  4.   

    我的createAndShowGUI()接口没写完
      

  5.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class LunarPhases extends JFrame {
    JPanel mainPane,selectPane,displayPane;
    JComboBox comboBox;
    ImageIcon[] icons = {new ImageIcon("c:/image0.jpg"),
    new ImageIcon("c:/image1.jpg"),
    new ImageIcon("c:/image2.jpg"),
    new ImageIcon("c:/image3.jpg"),
    new ImageIcon("c:/image4.jpg"),
    new ImageIcon("c:/image5.jpg"),
    new ImageIcon("c:/image6.jpg"),
    new ImageIcon("c:/image7.jpg"),
    new ImageIcon("c:/image8.jpg")
    };
    JLabel iconLabel ;
    String[] items = {"0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8"
    };
    public LunarPhases() {
    super("Lunar Phases");
    }
    public Component creatComponent() {
    mainPane = new JPanel(new GridLayout(2,1));//create and set the mainpane
    mainPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    mainPane.setSize(100,100);
    selectPane = new JPanel(new GridLayout(0,1));//create and set the selectpane
    selectPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createEmptyBorder(5,5,5,5),
    "Select Phase"));
    selectPane.setSize(100,50);
    comboBox = new JComboBox(items);//create and set the combobox and add it on  
    comboBox.addActionListener(comboBoxL);
    selectPane.add(comboBox);
    displayPane =new JPanel(new GridLayout(0,1));//create and set the displaypane
    displayPane.setBorder(BorderFactory.createTitledBorder(
    BorderFactory.createEmptyBorder(5,5,5,5),
    "Select Phase"));
    displayPane.setSize(100,100);
    iconLabel = new JLabel(icons[0]);//add the icon
    displayPane.add(iconLabel);
    mainPane.add(selectPane);
    return mainPane.add(displayPane);
    }
    ActionListener comboBoxL = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if("comboBoxChanged".equals(e.getActionCommand())) {
    iconLabel.setIcon(icons[comboBox.getSelectedIndex()]);
    }
    }
    };
    public void createAndShowGUI() {
    setDefaultLookAndFeelDecorated(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
    }
    public static void main(String args[]) {
    new LunarPhases().createAndShowGUI();
    }
    }
    这样就对了
      

  6.   

    createAndShowGUI()里还要添句
     getContentPane().add(createComponent()),才有显示