import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shijian extends JFrame
{
JButton bt1,bt2;
JPanel pl;
JLabel lblevent;
public Shijian()
{
super("Window Title");//实现父类的构造方法
pl=new JPanel();
bt1=new JButton("Button1");
bt2=new JButton("Button2");
lblevent=new JLabel("");
this.getContentPane().add(pl);
pl.add(bt1);
pl.add(bt2);
pl.add(lblevent);
listen blisten=new listen();//创建事件监听对象
bt1.addComponentListener(blisten);//为控件注册监听对象
bt2.addComponentListener(blisten);
this.setSize(200,200);
this.setVisible(true);
}
class listen implements WindowListener//自定义事件监听类以实现动作监听接口(该类为类中类)
{
public void windowClosed(WindowEvent evt)//重写ActionEvent事件处理方法
{
Object obj1=evt.getSource();//捕获事件源对象
if(obj1==bt1)
{
lblevent.setText("Button1 Clicked");//重新设置事件源对象的文本内容
}
}
public void windowOpened(WindowEvent evt)
{
}
public void windowClosing(WindowEvent evt)
{
}
public void windowActivated(WindowEvent evt)
{
}
public void windowDeactivated(WindowEvent evt)
{
Object obj2=evt.getSource();
if(obj2==bt2)
{
lblevent.setText("Button2 Clicked ");
}
}
public void windowIconified(WindowEvent evt)
{
}
public void windowDeiconified(WindowEvent evt)
{
}
}
public static void main(String args[])
{
Shijian f=new Shijian();
}
}
应该怎么用啊! 我写的有错