2015-11-16 55 views
0

我正在嘗試創建和編輯JComboBox以允許用戶輸入要購買的歌曲的名稱。但是,當我設置tunes.setEditable(true);我收到一個錯誤...任何幫助將不勝感激!可編輯JComboBox給出錯誤:<identifier>預期和非法開始類型

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.JTextArea; 

    public class JTunes2 extends JFrame implements ItemListener 
    { 
     int songNum,songPrice; 
     int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9}; 

     String result; 

     JComboBox tunes = new JComboBox(); 

     // set as editable 
     tunes.setEditable(true); 

     JLabel labelTunes = new JLabel("Song List"); 
     JLabel outputs = new JLabel(); 

     FlowLayout layout = new FlowLayout(); 

    public JTunes2() 
    { 
     super("Song Selector"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(); 
     panel.setLayout(layout); 

     // add song names to combo box and register an item listener. 

     tunes.addItem("Song1"); 
     tunes.addItem("Song2"); 
     tunes.addItem("Song3"); 
     tunes.addItem("Song4"); 
     tunes.addItem("Song5"); 
     tunes.addItem("Song6"); 
     tunes.addItem("Song7"); 
     tunes.addItem("Song8"); 
     tunes.addItem("Song9"); 
     tunes.addItem("Song10"); 
     tunes.addItem("Song11"); 
     tunes.addItem("Song12"); 
     tunes.addItemListener(this); 


     panel.add(labelTunes); 
     panel.add(tunes); 

     panel.add(outputs); 

    //add panel to the frame 
     setContentPane(panel); 


    } 

    public void itemStateChanged(ItemEvent e) 
    { 
     //create source object 
     Object source = e.getSource(); 

     //check the type size 
     if(source == tunes) 
     { 
     songNum = tunes.getSelectedIndex(); 
     songPrice = songAmount[songNum]; 
     result = "Total Price $" + songPrice; 

     //Display result 
     outputs.setText(result); 
     } 


    } 
    public static void main(String[] args) 
     { 
     // create class object 
     JTunes frame = new JTunes(); 
     frame.setSize(250, 180); 
     frame.setVisible(true); 
     } 
} 

謝謝!

回答

0

您在類級別而不是方法級別添加了tunes.setEditable(true)。在課堂上沒有任何陳述!

下面是一個固定版本:我將JTunes2重命名爲JTunes以修復編譯錯誤,並將setEditable移至構造函數。此外,我固定壓痕 - 這使得它更難犯這樣的錯誤:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JTextArea; 

public class JTunes extends JFrame implements ItemListener 
{ 
    int songNum,songPrice; 
    int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9}; 

    String result; 

    JComboBox tunes = new JComboBox(); 

    JLabel labelTunes = new JLabel("Song List"); 
    JLabel outputs = new JLabel(); 

    FlowLayout layout = new FlowLayout(); 

    public JTunes() 
    { 
     super("Song Selector"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel panel = new JPanel(); 
     panel.setLayout(layout); 

     tunes.setEditable(true); 

     // add song names to combo box and register an item listener. 

     tunes.addItem("Song1"); 
     tunes.addItem("Song2"); 
     tunes.addItem("Song3"); 
     tunes.addItem("Song4"); 
     tunes.addItem("Song5"); 
     tunes.addItem("Song6"); 
     tunes.addItem("Song7"); 
     tunes.addItem("Song8"); 
     tunes.addItem("Song9"); 
     tunes.addItem("Song10"); 
     tunes.addItem("Song11"); 
     tunes.addItem("Song12"); 
     tunes.addItemListener(this); 


     panel.add(labelTunes); 
     panel.add(tunes); 

     panel.add(outputs); 

     //add panel to the frame 
     setContentPane(panel); 
    } 

    public void itemStateChanged(ItemEvent e) 
    { 
     //create source object 
     Object source = e.getSource(); 

     //check the type size 
     if(source == tunes) 
     { 
      songNum = tunes.getSelectedIndex(); 
      songPrice = songAmount[songNum]; 
      result = "Total Price $" + songPrice; 

      //Display result 
      outputs.setText(result); 
     } 
    } 

    public static void main(String[] args) 
    { 
     // create class object 
     JTunes frame = new JTunes(); 
     frame.setSize(250, 180); 
     frame.setVisible(true); 
    } 
} 
1

事實上,Java要求在構造函數中,你設置JComponents。爲了讓你的代碼工作,你需要在構造函數中調用setEditable(true),這意味着你只需要移動tunes.setEditable(true);到構造函數。

提示:始終在構造函數中爲JComponents分配內存(您希望在創建Jframe時儘快繪製組件)。您可以在課堂級別上參考JComboBox。

這裏是你的代碼的另一個版本:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JTextArea; 

public class JTunes2 extends JFrame implements ItemListener 
{ 
    int songNum,songPrice; 
    int[] songAmount = {2,5,8,1,4,7,12,10,11,3,6,9}; 

    String result; 

    JComboBox tunes; 



    JLabel labelTunes = new JLabel("Song List"); 
    JLabel outputs = new JLabel(); 

    FlowLayout layout = new FlowLayout(); 

public JTunes2() 
{ 
    super("Song Selector"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel(); 
    panel.setLayout(layout); 

    tunes = new JComboBox(); 
    // set as editable 
    tunes.setEditable(true); 

    // add song names to combo box and register an item listener. 

    tunes.addItem("Song1"); 
    tunes.addItem("Song2"); 
    tunes.addItem("Song3"); 
    tunes.addItem("Song4"); 
    tunes.addItem("Song5"); 
    tunes.addItem("Song6"); 
    tunes.addItem("Song7"); 
    tunes.addItem("Song8"); 
    tunes.addItem("Song9"); 
    tunes.addItem("Song10"); 
    tunes.addItem("Song11"); 
    tunes.addItem("Song12"); 
    tunes.addItemListener(this); 


    panel.add(labelTunes); 
    panel.add(tunes); 

    panel.add(outputs); 

//add panel to the frame 
    setContentPane(panel); 


} 


public void itemStateChanged(ItemEvent e) 
{ 
    //create source object 
    Object source = e.getSource(); 

    //check the type size 
    if(source == tunes) 
    { 
    songNum = tunes.getSelectedIndex(); 
    songPrice = songAmount[songNum]; 
    result = "Total Price $" + songPrice; 

    //Display result 
    outputs.setText(result); 
    } 


} 
public static void main(String[] args) 
    { 
    // create class object 
    JTunes2 frame = new JTunes2(); 
    frame.setSize(250, 180); 
    frame.setVisible(true); 
    } 
} 
相關問題