2013-03-05 39 views
1

有人可以幫助我嗎?我有一個錯誤非法開始類型錯誤在行});我很困惑如何解決這個問題。任何幫助將不勝感激。該代碼如下所示:非法啓動類型錯誤

public SubokUlit(){ 
    String mgaPagkainTo[] = {"PM1 (Paa/ Spicy Paa with Thigh part)","PM2 (Pecho)","PM3 (Pork Barbeque 4 pcs.)","PM4 (Bangus Sisig)","PM5 (Pork Sisig)","PM6 (Bangus Inihaw)","SM1 (Paa)","SM2 (Pork Barbeque 2 pcs.)","Pancit Bihon","Dinuguan at Puto","Puto","Ensaladang Talong","Softdrinks","Iced Tea","Halo-Halo","Leche Flan","Turon Split"}; 
    JFrame frame = new JFrame("Mang Inasal Ordering System"); 
    JPanel panel = new JPanel(); 
    combo = new JComboBox(mgaPagkainTo); 
    combo.setBackground(Color.gray); 
    combo.setForeground(Color.red); 
    panel.add(combo); 
    frame.add(panel); 

    combo.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      String str = (String)combo.getSelectedItem(); 
      a = str; 
      if(a == "PM1 (Paa/ Spicy Paa with Thigh part)"){ 
       Wew(); 
      } 
      else if(a == "PM2 (Pecho)"){ 
       Wew1(); 
      } 
     }); // I am getting an error in this line 
    } 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(300,100); 
    frame.setVisible(true); 
} 

回答

4

);是放錯了地方:它應該是}下一行後:

combo.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     String str = (String)combo.getSelectedItem(); 
     a = str; 
     // Comparing strings should use equals, not == 
     if(a.equals("PM1 (Paa/ Spicy Paa with Thigh part)")){ 
      Wew(); 
     } else if(a.equals("PM2 (Pecho)")){ 
      Wew1(); 
     } 
    } // <<== Not here: this brace ends the method 
}); // <<== It should be after the brace that ends the anonymous class 
+1

+1 - 我想下一個問題,我們將看到的是 「爲什麼我的WEW/Wew1方法從來沒有所謂的」;) – 2013-03-05 10:49:23

+0

@Andreas是你'對了,這是我的下一個問題。我應該爲我的下一個問題開始新的討論嗎? – 2013-03-05 10:56:16

+1

多數民衆贊成沒有必要 - 見http://stackoverflow.com/questions/767372/java-string-equals-versus – 2013-03-05 10:57:19

1

}); // I am getting an error in this line 
} 

更改代碼

} // I am getting an error in this line 
}); 
^ 
0

做到這一點

} // I am getting an error in this line 
}); 

,而不是這樣的:

}); // I am getting an error in this line 
} 
相關問題