![](/img/trans.png)
[英]Zebra.java:3: error: call to super must be first statement in constructor
[英]Call to super must be first statement error
我正在嘗試編譯它,它不斷散出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;
}
該錯誤很明顯。
更改為:
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()的構造函數,則不會通過編譯。
所有匿名內部類似乎都包含生成的代碼,就好像您要反編譯現有的類文件(我懷疑您擁有的文件)一樣。 每次您有這樣的事情:
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
注釋。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.