
//名为Main的一个类
public class Main {
//主方法
public static void main(String[] args) {
}
//名为main的无参静态方法
public static void main() {
}
//名为main的有参静态方法
public static void main(int n) {
}
}
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("窗口")
}
}
publicstatic
void
main(String[]
args)throws
Exception
{}
main方法被声明为静态方法(static),一般在一个类里面定义这样的main方法对该类进行一些简单的测试。main方法就这一种声明格式。
1.public
定义main方法的访问权限为公有的。
2.static
声明为静态方法,静态方法是属于类的,在没实例化之前就可以使用。
3.void
声明返回方式。
4.如果在main方法里面会抛出异常且未当场处理时需加上throws
Exception,否则可以不用,Exception
可以是你自己定义的异常。
5.重点介绍一下main函数的参数
String[]
args
:
String[]
args是main函数的形式参数,可以用来获取命令行用户输入进去的参数。声明main函数的时候一定要在括号里写上它,否则会报错。
举个用到String[]
args的例子,可能不能运行,权当给你理解用。
class
Example
{
public
void
main(String[]
args)
{
System.out.println(args[0])
System.out.println(args[1])
}
}
那么这个程序在执行的时候就需要向main函数传递形式参数的值,好让main函数完成打印的功能。
注:String[]
args显示args是一个String类型的数组,args[0]、args[1]是头两个元素。
执行时在控制台输入:straul
good
后面两个字符串随便写什么,它们作为实参传入main函数,straul传入args[0],good传入args[1]。
那么屏幕会出现:
straul
good
这和在main函数里调用其他函数并且向被调用函数传递参数是一样的道理,只不过传给main函数罢了。
希望对你有用!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)