java处理按钮点击事件的方法

作者:jingxian 时间:2021-08-24 16:28:10 

不同的事件源可以产生不同类别的事件。例如,按钮可以发送一个ActionEvent对象,而窗口可以发送WindowEvent对象。

AWT时间处理机制的概要:

1. * 对象是一个实现了特定 * 接口(listener interface)的类的实例。

2. 事件源是一个能够注册 * 对象并发送事件对象的对象。

3. 当事件发生时,事件源将事件对象传递给所有注册的 * 。

4. * 对象将利用事件对象中的信息决定如何对事件做出响应。

下面是 * 的一个示例:


ActionListener listener = ...;
JButton button = new JButton("OK");
button.addActionListener(listener);

现在,只要按钮产生了一个“动作事件”,listener对象就会得到通告。对于按钮来说,正像我们想到的,动作事件就是点击按钮。

为了实现ActionListener接口, * 类必须有一个被称为actionPerformed的方法,该方法接收一个ActionEvent对象参数。


class MyListener implements ActionListener
{
...;
public void actionPerformed(ActionEvent event)
{
  //reaction to button click goes here
}
}

只要用户点击了按钮,JButton对象就会创建一个ActionEvent对象,然后调用listener.actionPerformed(event)传递事件对象。可以将多个 * 对象添加到一个像按钮这样的事件源中。这样一来,只要用户点击按钮,按钮就会调用所有 * 的actionPerformed方法。

实例:处理按钮点击事件

为了加深对事件委托模型的理解,下面以一个响应按钮点击事件的简单示例来说明所需要知道的细节。在这个示例中,想要在一个面板中放置三个按钮,添加三个 * 对象用来作为按钮的动作 * 。

在这个情况下,只要用户点击面板上的任何一个按钮,相关的 * 对象就会接收到一个ActionEvent对象,它表示有个按钮被点击了。在示例程序中, * 对象将改变面板的背景颜色。

在演示如何监听按钮点击事件之前,首先需要讲解一下如何创建按钮以及如何将他们添加到面板中。

可以通过在按钮构造器中指定一个标签字符串、一个图标或两项都指定来创建一个按钮。下面是两个示例:


JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));

将按钮添加到面板中需要调用add方法:


JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

至此,知道了如何将按钮添加到面板上,接下来需要增加让面板监听这些按钮的代码。这需要一个实现了ActionListener接口的类。如前所述,应该包含一个actionPerformed方法,其签名为:

public void actionPerformed(ActionEvent event)

当按钮被点击时,希望将面板的背景颜色设置为指定的颜色。这个颜色存储在 * 类中:


class ColorAction implements ActionListener
{
 public ColorAction(Color c)
 {
  backgroundColor = c;
 }
 public void actionPerformed(actionEvent event)
 {
  //set panel background color
  }
  private Color backgroundColor;
}

然后,为每种颜色构造一个对象,并将这些对象设置为按钮 * 。


ColorAction yelloAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);

例如,如果一个用户在标有“Yellow”的按钮上点击了一下,yellowAction对象的actionPerformed方法就会被调用。这个对象的backgroundColor实例域被设置为Color.YELLOW,现在就将面板的背景颜色设置为黄色。

这里还有一个需要考虑的问题。ColorAction对象不能访问buttonpanel变量。可以采用两种方式解决这个问题。一个是将面板存储在ColorAction对象中,并在ColorAction的构造器中设置它;另一个是将ColorAction作为ButtonPanel类的内部类,如此,它的方法就自动地拥有访问外部面板的权限了。

下面说明一下如何将ColorAction类放在ButtonFrame类内。


class ButtonFrame extends JFrame
{
...
private class ColorAction implents ActionListener
{
 ...
 public void actionPerformed(ActionEvent event)
 {
   buttonPanel.setBackground(backgroundColor);
 }
 private Color backgroundColor;
 }
 private Jpanel buttonPanel;
}
标签:java,按钮,点击事件
0
投稿

猜你喜欢

  • 解决jmap命令打印JVM堆信息异常的问题

    2023-11-05 09:07:02
  • C#实现过滤sql特殊字符的方法集合

    2022-01-30 23:58:04
  • Java传入用户名和密码并自动提交表单实现登录到其他系统的实例代码

    2023-09-20 00:40:46
  • 代理角色java设计模式之静态代理详细介绍

    2022-10-14 16:48:03
  • Java中的 FilterInputStream简介_动力节点Java学院整理

    2023-01-21 17:18:56
  • java实现多文件上传至本地服务器功能

    2022-01-26 11:55:24
  • java 类加载机制和反射详解及实例代码

    2023-11-30 06:42:20
  • Javaweb开发中通过Servlet生成验证码图片

    2022-06-23 06:33:34
  • Java 导出Excel增加下拉框选项

    2021-10-13 07:58:50
  • C#中的一些延时函数

    2023-11-29 04:33:30
  • Java代码块与代码加载顺序原理详解

    2023-06-03 12:56:42
  • Java实现插入排序算法可视化的示例代码

    2021-08-06 19:35:50
  • Java实现人机猜拳游戏

    2023-10-16 08:47:56
  • Android 文件读写操作方法总结

    2023-12-22 22:52:29
  • Java基础之异常处理详解

    2021-07-14 03:03:49
  • Unity UGUI教程之实现滑页效果

    2022-06-30 10:38:30
  • java ReentrantLock条件锁实现原理示例详解

    2023-12-12 02:36:13
  • c#调用存储过程实现登录界面详解

    2023-01-02 22:03:07
  • spring项目中切面及AOP的使用方法

    2021-12-01 21:11:29
  • Android中ACTION_CANCEL的触发机制与滑出子view的情况

    2023-08-01 14:39:09
  • asp之家 软件编程 m.aspxhome.com