java核心技术卷 之处理按钮点击事件
来源:互联网 发布:安徽seo公司 编辑:程序博客网 时间:2024/06/07 01:51
为了加深对事件委托模铟的理解,下面以一个响应按钮点击事件的简单示例来说明所需要知道的所有细节。在这个示例中,想要在一个面板中放置三个按钮,添加三个监听器对象用来作为按钮的动作监听器。
在这个情况下,用户点击面板上的任何一个按钮,相关的监听器对象就会接收到一 个ActionEvent对象,它表示有个按钮被点击了在示例程序中,监听器对象将改变面板的背景颜色。
在演示如何监听按钮点击事件之前,首先需要讲解一下如何创建按钮以及如何将它们添加到而板中。可以通过在按钮构造器中指定一个标签字符串、一个图标或两项都指定来创建一个按钮。下面是两个示例:
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
将按钮添加到面板中需要凋用add方法:
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
接下来需要增加让面板监听这些按钮的代码。这需要一个实现了ActicnListener接口的类。如前所述,应该包含一个actionPerformed方法,其签名为:
public void actionPerformed(ActionEvent event)
当按钮被点击时,希望将而板的背累颜色设置为指定的颜色。这个颜色存储在监听器类中:
private class ColorAction implements ActionListener{
private Color BackgrpundColor;
public ColorAction(Color c){
BackgrpundColor=c;
}
public void actionPerformed(ActionEvent event){
buttonPanel.setBackground(BackgrpundColor);
}
}
然后,为每种颜色构造一个对象,并将这些对象设置为按钮监听器。
//create button actions
ColorAction yellowAction=new ColorAction(Color.YELLOW);
ColorAction blueAction=new ColorAction(Color.BLUE);
ColorAction redAction=new ColorAction(Color.red);
//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
例如,如果一个用户在标有‚Yellow的按钮上点击了一下,yellowAction对象的actionPerformed方法就会被调用。这个对象的backgroundColor实例域被设控为Color.YELLOW, 现在就将面板的背景色设置为黄色了。
这里还有一个需要考虑的问题。ColorAction对象不能访问buttonpane变量。可以采用两种方式解决这个问题。一个是将面板存储在ColorAction对象中,并在ColorAction的构造器中设置它;另一个是将ColorAction作为ButtonFrame类的内部类,这样一来,它的方法就自动地拥有访问外部面板的权限了。
完整的代码:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
/**
* Created by IBM on 2017/9/11.
*/
public class ButtonFrame extends JFrame {
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public static void main(String[]args){
ButtonFrame buttonFrame=new ButtonFrame();
buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonFrame.setVisible(true);
}
public ButtonFrame(){
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("Red");
buttonPanel=new JPanel();
//add buttons to panel
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
//create button actions
ColorAction yellowAction=new ColorAction(Color.YELLOW);
ColorAction blueAction=new ColorAction(Color.BLUE);
ColorAction redAction=new ColorAction(Color.red);
//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener{
private Color BackgrpundColor;
public ColorAction(Color c){
BackgrpundColor=c;
}
public void actionPerformed(ActionEvent event){
buttonPanel.setBackground(BackgrpundColor);
}
}
}
运行结果:
- java核心技术卷 之处理按钮点击事件
- java核心技术卷 之事件处理基础
- 事件处理基础 处理按钮点击事件实例 Java核心技术
- 事件处理(Java核心技术卷Ⅰ)
- java核心技术卷 之单选按钮
- Java核心技术:卷1笔记[6] 事件处理
- #Java 核心技术卷一阅读笔记# 第十一章 事件处理
- java核心技术卷 之JFrame
- java核心技术卷 之菜单
- java核心技术卷 之工具栏
- java核心技术 事件处理
- Java核心技术(事件处理)
- java核心技术笔记 事件处理
- java核心技术卷 之创建框架
- java核心技术卷 之框架属性
- java核心技术卷 之布局管理概述
- java核心技术卷 之网格布局
- java核心技术卷 之文本输入
- python3 数据操作简单封装
- 如何避免数据库“勒索事件”和“从删库到跑路”的尴尬
- 阿里云centos7.03源码安装freeswitch
- 9/10 javaScript数组游戏
- 设计模式-单例模式
- java核心技术卷 之处理按钮点击事件
- 后台传输int型数据到jsp乱码的问题
- String中的intern方法
- 分库分表都有哪些实现[待编辑]
- 持续集成之代码质量管理-Sonar [三]
- 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
- android String文件中修改部分字体颜色
- 快速查看&关掉冲突的端口
- java 生成.h文件遇到的问题