温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-17 07:17:02 来源:脚本之家 阅读:321 作者:jingxian 栏目:编程语言

不同的事件源可以产生不同类别的事件。例如,按钮可以发送一个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处理按钮点击事件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI