求助 !急,这句错在哪里?

yasu1984 2005-10-25 02:27:32
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 错误
大侠帮我解决一下,谢谢了
...全文
120 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanxiazhiqiu 2005-10-25
  • 打赏
  • 举报
回复
楼主,稍安毋躁呵呵!
yasu1984 2005-10-25
  • 打赏
  • 举报
回复
createAndShowGUI()里还要添句
getContentPane().add(createComponent()),才有显示
boyu_song 2005-10-25
  • 打赏
  • 举报
回复

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


这样就对了
yasu1984 2005-10-25
  • 打赏
  • 举报
回复
我的createAndShowGUI()接口没写完
yasu1984 2005-10-25
  • 打赏
  • 举报
回复
我草,可能是我工作了6个小时,头发昏了,竟然出这个错
believefym 2005-10-25
  • 打赏
  • 举报
回复
creatComponent没有返回值,当然有错,要么程序return一个component,要么改成void,看你的需要
yasu1984 2005-10-25
  • 打赏
  • 举报
回复
我全贴出来啊,我手写的一个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();
}
}





眼急手块 2005-10-25
  • 打赏
  • 举报
回复
对啊,代码贴少了。BTW,if("comboBoxChanged".equals(e.getActionCommand())) 这样写看了很不习惯啊。。。if(e.getActionCommand().equals("comboBoxChanged"))这样如何?呵呵
believefym 2005-10-25
  • 打赏
  • 举报
回复
代码贴全了看看

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧