这段代码向我显示语法错误(“ {”除外),但我认为还有其他错误。

 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) {


它看起来像个foreach,因此应该是:

for (Product name:Application.manu) {

10-07 12:27