2

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

 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来设置标签吗?

4

1 回答 1

2

编译错误实际上与这一行有关:

(Product name:Application.manu) {

它看起来像一个 foreach,所以它应该是:

for (Product name:Application.manu) {
于 2013-02-09T09:23:20.570 回答