事件监听

package 事件监听;
import java.awt.*;
import java.awt.event.*;

public  class Frame2 extends Frame implements ActionListener{
 private Button button1;
 public Frame2()
 {
  super("个人信息");
      this.setSize(250,200);//设计组件的尺寸
      this.setLocation(800,600);//设计组件显示的位置
      this.setBackground(Color.blue);//设计背景颜色
      this.setLayout(new FlowLayout());//设计容器为流布局,居中
      this.add(new Label("姓名"));
      this.add(new TextField("浩杰”,20));
      this.add(new Label("性别"));
      this.add(new TextField("男",20));
      this.add(new Label("民族"));
      this.add(new TextField("藏",20));
      this.add(new Label("年龄"));
      this.add(new TextField("23",20));
      button1=new Button("确认");
      this.add(button1);
      button1.addActionListener(this);
      this.addWindowListener(new WinClose());
      this.setVisible(true);
 }
 public void actionPerformed(ActionEvent ev)
 {
  if(ev.getSource()==button1)
  {
   System.out.print("welcome");
  }
 }
 public static void main(String arg[])
 {
  new Frame2();
 }
     class WinClose implements WindowListener
 {
  public void windowClosing(WindowEvent ev)
  {
   System.exit(0);
  }
  public void windowOpened(WindowEvent ev) {}
  public void windowActivated(WindowEvent ev) {}
  public void windowDeactivated(WindowEvent ev) {}
  public void windowClose(WindowEvent ev) {}
  public void windowIconified(WindowEvent ev) {}
  public void windowDeiconified(WindowEvent ev) {}
  @Override
  public void windowClosed(WindowEvent arg0) {
   // TODO 自动生成的方法存根
   
  }
 } 
 }

你可能感兴趣的:(事件监听)