import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
public class EventModelDemo implements ActionListener{
JLabel tips=new JLabel("請單擊下面的按鈕");
JButton b1=new JButton("確定");
JButton b2=new JButton("取消");
public static void main(String[]args){
EventModelDemo demo=new EventModelDemo();
demo.initUI();
}
void initUI(){
BaseFrame f=new BaseFrame("事件處理模型演示");
f.setLayout(new FlowLayout());
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tips);
f.add(b1);
f.add(b2);
f.showMe();
}
public void actionPerform(ActionEvent e){
if(e.getSource()==b1){
tips.setText("你單擊了”"+b1.getText()+"”");
}else if(e.getSource()==b2){
tips.setText("你單擊了“"+b2.getText()+"”");
}
}
}
在EventModelDemo出提示:The type EventModelDemo must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
你繼承自ActionListener接口,所以你要實現ActionListener接口中的方法,你肯定有的方法還未實現