b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ea){
System.exit(0);
}
});
我正在学习Java并看到上面的代码.
我无法理解为什么addActionlisetner方法需要Actionlistener作为参数.
使用System.exit(0)不是更简单吗?
解决方法:
您可以使用API Java作为参考来查找问题的答案.
public void addActionListener(ActionListener l)
Adds an ActionListener to the button.
Parameters:
l – the ActionListener to be added
例如,具体类JButton从类javax.swing.AbstractButton继承了方法addActionListener(ActionListener l).
当你这样做时:
new ActionListener(){
public void actionPerformed(ActionEvent ea){
System.exit(0);
}
}
您正在创建ActionListener的anonymous subclass的实例.
ActionListener是用于接收动作事件的接口.
The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component’s addActionListener method. When the action event occurs, that object’s actionPerformed method is invoked.
标签:actionlistener,system-exit,java,arguments
来源: https://codeday.me/bug/20190829/1760067.html