java组建事件的实例说明

第一种方法:

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Listent extends JFrame implements ActionListener {

 private JButton btBlue, btDialog;

 // 构造方法
 public Listent() {
  // 设置标题栏内容
  setTitle("Java GUI 事件监听处理");
  // 设置初始化窗口位置
  setBounds(100, 100, 500, 350);
  // 设置窗口布局
  setLayout(new FlowLayout());
  // 创建按钮对象
  btBlue = new JButton("蓝色");
  // 将按钮添加事件监听器
  btBlue.addActionListener(this);
  // 创建按钮对象
  btDialog = new JButton("弹窗");
  // 将按钮添加事件监听器
  btDialog.addActionListener(this);
  // 把按钮容器添加到JFrame容器上
  add(btBlue);
  add(btDialog);
  // 设置窗口可视化
  setVisible(true);
  // 设置窗口关闭
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 // ***************************事件处理***************************
 @Override
 public void actionPerformed(ActionEvent e) {
  // 判断最初发生Event事件的对象
  if (e.getSource() == btBlue) {
   // 获得容器
   Container c = getContentPane();
   // 设置容器背景颜色
   c.setBackground(Color.BLUE);
  } else if (e.getSource() == btDialog) {
   // 创建JDialog窗口对象
   JDialog dialog = new JDialog();
   dialog.setBounds(300, 200, 400, 300);
   dialog.setVisible(true);
  }
 }

 // ***************************主方法***************************
 public static void main(String[] args) {
  new Listent();
 }

}

第二种,通过匿名类处理。这是比较好的一种方法,我是在2011年开始使用这种方法的。

public class Listen2 extends JFrame {

 private JButton btBlue, btDialog;

 // 构造方法
 public Listen2() {
  // 设置标题栏内容
  setTitle("Java GUI 事件监听处理");
  // 设置初始化窗口位置
  setBounds(100, 100, 500, 350);
  // 设置窗口布局
  setLayout(new FlowLayout());
  // 创建按钮对象
  btBlue = new JButton("蓝色");
  // 添加事件监听器(此处即为匿名类)
  btBlue.addActionListener(new ActionListener() {
   // 事件处理
   @Override
   public void actionPerformed(ActionEvent e) {
    // 获得容器,设置容器背景颜色
    Container c = getContentPane();
    c.setBackground(Color.BLUE);
   }
  });
  // 创建按钮对象,并添加事件监听器
  btDialog = new JButton("弹窗");
  btDialog.addActionListener(new ActionListener() {
   // 事件处理
   @Override
   public void actionPerformed(ActionEvent e) {
    // 创建JDialog窗口对象
    JDialog dialog = new JDialog();
    dialog.setBounds(300, 200, 400, 300);
    dialog.setVisible(true);
   }
  });

  // 把按钮容器添加到JFrame容器上
  add(btBlue);
  add(btDialog);
  // 设置窗口可视化
  setVisible(true);
  // 设置窗口关闭
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 // ***************************主方法***************************
 public static void main(String[] args) {
  new Listen2();
 }

}

第三种:通过内部类处理。
//该种方法更符合面向对象编程的思想。

 private JButton btBlue, btDialog;

 // 构造方法
 public Listen3() {
  // 设置标题栏内容
  setTitle("Java GUI 事件监听处理");
  // 设置初始化窗口位置
  setBounds(100, 100, 500, 350);
  // 设置窗口布局
  setLayout(new FlowLayout());
  // 创建按钮对象
  btBlue = new JButton("蓝色");
  // 添加事件监听器对象(面向对象思想)
  btBlue.addActionListener(new ColorEventListener());
  btDialog = new JButton("弹窗");
  btDialog.addActionListener(new DialogEventListener());
  // 把按钮容器添加到JFrame容器上
  add(btBlue);
  add(btDialog);
  // 设置窗口可视化
  setVisible(true);
  // 设置窗口关闭
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 // 内部类ColorEventListener,实现ActionListener接口
 class ColorEventListener implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
   Container c = getContentPane();
   c.setBackground(Color.BLUE);
  }

 }

 // 内部类DialogEventListener,实现ActionListener接口
 class DialogEventListener implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
   // 创建JDialog窗口对象
   JDialog dialog = new JDialog();
   dialog.setBounds(300, 200, 400, 300);
   dialog.setVisible(true);
  }

 }

 // ***************************主方法***************************
 public static void main(String[] args) {
  new Listen3();
 }

}
public class Listen4 extends JFrame {
// 第四种:通过外部类处理
 private JButton btBlue, btDialog;

 // 构造方法
 public Listen4() {
  // 设置标题栏内容
  setTitle("Java GUI 事件监听处理");
  // 设置初始化窗口位置
  setBounds(100, 100, 500, 350);
  // 设置窗口布局
  setLayout(new FlowLayout());
  // 创建按钮对象
  btBlue = new JButton("蓝色");
  // 将按钮添加事件监听器
  btBlue.addActionListener(new ColorEventListener(this));
  // 创建按钮对象
  btDialog = new JButton("弹窗");
  // 将按钮添加事件监听器
  btDialog.addActionListener(new DialogEventListener());
  // 把按钮容器添加到JFrame容器上
  add(btBlue);
  add(btDialog);
  // 设置窗口可视化
  setVisible(true);
  // 设置窗口关闭
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

 // ***************************主方法***************************
 public static void main(String[] args) {
  new Listen4();
 }

}

// 外部类ColorEventListener,实现ActionListener接口
class ColorEventListener implements ActionListener {

 private Listen4 el;

 ColorEventListener(Listen4 el) {
  this.el = el;
 }

 @Override
 public void actionPerformed(ActionEvent e) {
  Container c = el.getContentPane();
  c.setBackground(Color.BLUE);
 }

}

// 外部类DialogEventListener,实现ActionListener接口
class DialogEventListener implements ActionListener {

 @Override
 public void actionPerformed(ActionEvent e) {
  // 创建JDialog窗口对象
  JDialog dialog = new JDialog();
  dialog.setBounds(300, 200, 400, 300);
  dialog.setVisible(true);
 }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值