java错误 方法 main 不能声明为“静态”;只能在静态类型或顶级类型中才能声明静态

import java.awt.*;
import java.awt.event.*;
abstract class MyWindow extends Frame implements ItemListener,ActionListener
{
private static final long serialVersionUID = -5398818932510250367L;
Choice choice;
TextField text;
TextArea area;
Button add,del;
MyWindow(String s)
{setLayout(new FlowLayout());
choice= new Choice();
text=new TextField(8);
area=new TextArea(6,25);
choice.add("音乐");
choice.add("游戏");
add=new Button("添加");
del=new Button("删除");
add.addActionListener(this);
del.addActionListener(this);
text.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);
add(add);
add(text);
add(area);
setSize(200,200);
setVisible(true);
validate();

}

public void itemStateChangd(ItemEvent e)
{
String name=choice.getSelectedItem();
int index=choice.getSelectedIndex();
area.setText("\n"+index+":"+name);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==add||e.getSource()==text)
{ String name=text.getText();
if(name.length()>0)
{choice.add(name);
choice.select(name);
area.append("\n列表添加:"+name);
}
}
else if(e.getSource()==del)
{area.append("\n列表删除:"+choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}

public class test2
{

public static void main(String[] args)
{
MyWindow win=new MyWindow("窗口");
}

}
}

请高手看下代码哪里出现错误了???!!!
用的eclipse编译的哈,
错误1:方法 main 不能声明为“静态”;只能在静态类型或顶级类型中才能声明静态方法 第59行。
错误2:类型 MyWindow 必须实现继承的抽象方法 ItemListener.itemStateChanged(ItemEvent)第 3 行
谢谢大家的回答!!!!!!
我又调试了下,现在能运行了,自动快速修正在
“{area.append("\n列表删除:"+choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}”后面添加了:
“public void itemStateChanged(ItemEvent arg0) {
// TODO 自动生成方法存根

}

}

"
public class test2
{

public static void main(String args[])
{
new MyWindow();
}

}
就可以调试了,但是还是不太清楚是怎么回事??

第1个回答  推荐于2016-08-27
1,你外面的类是一个抽象类,里面又有一个公共类,
2,main方法是要在非抽象类中才能使用的

解决后的代码:
public class MyWindow extends JFrame implements ItemListener, ActionListener {
private static final long serialVersionUID = -5398818932510250367L;
Choice choice;
TextField text;
TextArea area;
Button add, del;

MyWindow(String s) {
setLayout(new FlowLayout());
choice = new Choice();
text = new TextField(8);
area = new TextArea(6, 25);
choice.add("音乐");
choice.add("游戏");
add = new Button("添加");
del = new Button("删除");
add.addActionListener(this);
del.addActionListener(this);
text.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);
add(add);
add(text);
add(area);
setSize(200, 200);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == add || e.getSource() == text) {
String name = text.getText();
if (name.length() > 0) {
choice.add(name);
choice.select(name);
area.append("\n列表添加:" + name);
}
} else if (e.getSource() == del) {
area.append("\n列表删除:" + choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}

public void itemStateChanged(ItemEvent e) {
String name = choice.getSelectedItem();
int index = choice.getSelectedIndex();
area.setText("\n" + index + ":" + name);

}

public static void main(String[] args) {
new MyWindow("窗口");
}

}本回答被提问者采纳
第2个回答  2010-05-24
你这个是内部类,内部类中不能有静态方法
相似回答