我想編譯這個,它不斷吐出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;
}
這看起來像你反編譯的代碼 - 是什麼呢?如果是這樣,請注意,反編譯器不會一直吐出有效的代碼。您應該向作者詢問原始源代碼。 – 2014-12-03 07:08:58
[爲什麼this()和super()必須是構造函數中的第一條語句?](http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-在構造函數中的第一個語句) – BackSlash 2014-12-03 07:09:37