这段代码显示了语法错误("{"除外),但我认为其他地方出了问题。
public class CircleController extends JPanel {
private JComboBox product1=new JComboBox(Application.listArray);
private JLabel jlb1=new JLabel();
//..........some fields more
// constructor....
public CircleController() {
// Panel to group labels
JPanel panel1 = new JPanel();
//..........some labels....
//another panel
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,4,20,20 ));
panel2.add(product1);
panel2.add(jlb1);
和错误的部分代码
//product1 is combobox, jlbt is label wich I want to set when I selected item from combobox
product1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) { // in this line shows me an error
(Product name:Application.manu) {
if ((String)product1.getSelectedItem()==name.getName()){
String price1=Double.toString(name.getPrice());
jlb1.setText(price1);
}
}
if (model != null){
model.setProduct((String)product1.getSelectedItem());}}
});
该程序使用代码运行良好
product1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (model != null){
model.setProduct((String)product1.getSelectedItem());}}
});
但是当我添加这个语句时(这个语句被检查过并且是好的)
(Product name:Application.manu) {
if ((String)product1.getSelectedItem()==name.getName()){
String price1=Double.toString(name.getPrice());
jlb1.setText(price1);
}
}
product1.addItemListener(new ItemListener() {....
程序内部显示错误
我必须添加另一个ItemListener
来设置标签吗?
编译错误实际上与以下行有关:
(Product name:Application.manu) {
它看起来像前臂,所以应该是:
for (Product name:Application.manu) {