JAVA的一些问题!

来源:百度知道 编辑:UC知道 时间:2024/05/18 07:49:49
1、btn所产生的事件,用监听器接口的方法来实现;
2、点击应用程序的关闭按钮,产生关闭此程序的事件,用适配器类和内部类处理事件的方法来实现
3、在实例10-2中采用监听器接口中的方法处理关闭窗口事件,与在实例10-3中采用的适配器类和内部类来处理该事件,有何异同?为什么?
补充一下10-2 问题!!
//实例10-2
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class ButtonFrame extends Frame implements (1) {
Button btn=new Button("确认");
Label lb=new Label(" ");
TextField txt=new TextField(10);

ButtonFrame() {
super("实例10-2");
setLayout(new FlowLayout());
setBackground(Color.blue);
setSize(200,100);
lb.setForeground(Color.red);
(2) ;//给按钮注册动作监听器
add(txt);
add(btn);
add(lb);
setVisible(true);
}

//对点击“确认”按钮所做的事件处理
public void (3) (ActionEvent

(1) ActionListener
(2) btn.addActionListener(this);
(3) actionPerformed
(4) System.exit(0);

可以形成习惯:每当要实现接口就要在方法后面写出实现的接口的名字,如果是多个,就用逗号隔开。
在类体中,就要实现所有接口中的方法。
如果用的是适配器的话,就不用实现所有的方法了,根据新建的需要来实现

本题的答案应该是:
(1) ActionListener
(2) btn.addActionListener(this);
(3) actionPerformed
(4) System.exit(0);

答案是:(1) ActionListener
(2) btn.addActionListener(this);
(3) actionPerformed
(4) System.exit(0);
你自己在看看注意一下