2014-12-03 88 views
0

我想編譯這個,它不斷吐出6個錯誤,說明error:call to super must be first statement in constructor - 我真的很困惑,並沒有能夠糾正它。幫助將不勝感激。調用超級必須是第一個語句錯誤

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import javax.swing.*; 

public class MenuFrame extends JFrame 
{ 
private class ItemHandler 
    implements ActionListener 
{ 

    public void actionPerformed(ActionEvent actionevent) 
    { 
     if(fieldItems[0].isSelected()) 
      application.insert(textArea); 
     else 
     if(fieldItems[1].isSelected()) 
      application.delete(textArea); 
     if(fieldItems[2].isSelected()) 
      application.update(textArea); 
     repaint(); 
    } 

    final MenuFrame this$0; 

    private ItemHandler() 
    { 
     this$0 = MenuFrame.this; 
     super(); 
    } 

} 
    public MenuFrame() 
{ 
    super("Using JMenus"); 
    JMenu jmenu = new JMenu("File"); 
    jmenu.setMnemonic('F'); 
    JMenuItem jmenuitem = new JMenuItem("New"); 
    jmenuitem.setMnemonic('N'); 
    jmenu.add(jmenuitem); 
    jmenuitem.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent actionevent) 
     { 
      File file = getFile(2); 
      JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1); 
      application = new HardwareStore(file.toString(), 0); 
     } 

     final MenuFrame this$0; 


     { 
      this$0 = MenuFrame.this; 
      super(); 
     } 
    } 
); 
    JMenuItem jmenuitem1 = new JMenuItem("Open"); 
    jmenuitem1.setMnemonic('O'); 
    jmenu.add(jmenuitem1); 
    jmenuitem1.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent actionevent) 
     { 
      File file = getFile(1); 
      if(file.exists()) 
      { 
       JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1); 
       application = new HardwareStore(file.toString(), 1); 
      } 
     } 

     final MenuFrame this$0; 
{ 
      this$0 = MenuFrame.this; 
      super(); 
     } 
    } 
); 
    jmenu.addSeparator(); 
    JMenuItem jmenuitem2 = new JMenuItem("Exit"); 
    jmenuitem2.setMnemonic('x'); 
    jmenu.add(jmenuitem2); 
    jmenuitem2.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent actionevent) 
     { 
      System.exit(0); 
     } 

     final MenuFrame this$0; 


     { 
      this$0 = MenuFrame.this; 
      super(); 
     } 
    } 
); 
    JMenuBar jmenubar = new JMenuBar(); 
    setJMenuBar(jmenubar); 
    jmenubar.add(jmenu); 
    JMenu jmenu1 = new JMenu("Process"); 
    jmenu1.setMnemonic('P'); 
    String as[] = { 
     "Insert", "Delete", "Update" 
    }; 
    JMenu jmenu2 = new JMenu("Edit"); 
    jmenu2.setMnemonic('E'); 
    fieldItems = new JRadioButtonMenuItem[as.length]; 
    fieldButtonGroup = new ButtonGroup(); 
    ItemHandler itemhandler = new ItemHandler(); 
    for(int i = 0; i < as.length; i++) 
    { 
     fieldItems[i] = new JRadioButtonMenuItem(as[i]); 
     jmenu2.add(fieldItems[i]); 
     fieldButtonGroup.add(fieldItems[i]); 
     fieldItems[i].addActionListener(itemhandler); 
    } 
fieldItems[0].setSelected(true); 
    jmenu1.add(jmenu2); 
    jmenu1.addSeparator(); 
    JMenuItem jmenuitem3 = new JMenuItem("List"); 
    jmenuitem3.setMnemonic('L'); 
    jmenu1.add(jmenuitem3); 
    jmenuitem3.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent actionevent) 
     { 
      application.display(textArea); 
     } 

     final MenuFrame this$0; 


     { 
      this$0 = MenuFrame.this; 
      super(); 
     } 
    } 
    ); 
jmenubar.add(jmenu1); 
    JMenuItem jmenuitem4 = new JMenuItem("About..."); 
    jmenuitem4.setMnemonic('A'); 
    jmenubar.add(jmenuitem4); 
    jmenuitem4.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent actionevent) 
     { 
      JOptionPane.showMessageDialog(MenuFrame.this, "This is an example\nof using menus", "About", -1); 
     } 

     final MenuFrame this$0; 


     { 
      this$0 = MenuFrame.this; 
      super(); 
     } 
    } 
); 
    textArea = new JTextArea(); 
    add(textArea, "Center"); 
} 
private File getFile(int i) 
{ 
    JFileChooser jfilechooser = new JFileChooser(); 
    jfilechooser.setFileSelectionMode(0); 
    int j; 
    if(i == 1) 
     j = jfilechooser.showOpenDialog(this); 
    else 
     j = jfilechooser.showSaveDialog(this); 
    if(j == 1) 
     System.exit(1); 
    File file = jfilechooser.getSelectedFile(); 
    if(file == null || file.getName().equals("")) 
    { 
     JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", 0); 
     System.exit(1); 
    } 
    return file; 
} 
private JRadioButtonMenuItem fieldItems[]; 
private JRadioButtonMenuItem fonts[]; 
private JCheckBoxMenuItem styleItems[]; 
private JTextArea textArea; 
private JTextField recNumText; 
private JTextField newValueText; 
private JTextField fieldText; 
private ButtonGroup fontButtonGroup; 
private ButtonGroup fieldButtonGroup; 
private int style; 
private HardwareStore application; 

}

+1

這看起來像你反編譯的代碼 - 是什麼呢?如果是這樣,請注意,反編譯器不會一直吐出有效的代碼。您應該向作者詢問原始源代碼。 – 2014-12-03 07:08:58

+0

[爲什麼this()和super()必須是構造函數中的第一條語句?](http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-在構造函數中的第一個語句) – BackSlash 2014-12-03 07:09:37

回答

1

的錯誤是足夠清晰。

它改成這樣:

private ItemHandler() 
{ 
    super(); // call to super must be the first line of the constructor 
    this$0 = MenuFrame.this; 
} 

至於在實例初始化塊中的通話super,這樣的初始化語句塊複製到每個構造的開始,所以你必須做出同樣的變化:

{ 
     super(); 
     this$0 = MenuFrame.this; 
    } 

但是,如果具有此塊的類已具有調用super()的構造函數,則不會傳遞編譯。

+0

刪除自動生成的代碼以複製封閉的實例引用會更簡單 - 編譯器會生成它。 – 2014-12-03 07:12:29

0

所有的匿名內部類似乎都包含了生成的代碼,就好像你要反編譯一個現有的類文件(我懷疑你有)。每次你有這樣的事情:

jmenuitem1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent actionevent) 
    { 
     File file = getFile(1); 
     if(file.exists()) 
     { 
      JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1); 
      application = new HardwareStore(file.toString(), 1); 
     } 
    } 

    final MenuFrame this$0; 
    { 
     this$0 = MenuFrame.this; 
     super(); 
    } 
}); 

...你應該擺脫最後一部分 - 它會自動爲你生成。你只需要:

jmenuitem1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent actionevent) 
    { 
     File file = getFile(1); 
     if(file.exists()) 
     { 
      JOptionPane.showMessageDialog(MenuFrame.this, file.toString(), file.toString(), -1); 
      application = new HardwareStore(file.toString(), 1); 
     } 
    } 
}); 

(雖然我想補充一個@Override註解了。)

相關問題