我试图编译它并且它不断吐出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;
}
答案 0 :(得分: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()的构造函数,则不会传递编译。
答案 1 :(得分: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
注释。)