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 错误
大侠帮我解决一下,谢谢了
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 错误
大侠帮我解决一下,谢谢了
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();
}
}
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();
}
}
这样就对了
getContentPane().add(createComponent()),才有显示