查看“注册事件监听器和处理事件”的源代码
←
注册事件监听器和处理事件
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==事件处理== java采用一个基于委派的模型来进行事件处理:一个源对象触发一个事件,然后一个对该事件感兴趣的对象处理它。后者称为事件处理器或事件监听器。 一个对象如果要成为一个源对象的事件处理器,需要满足两个条件: #监听器对象必须是一个对应的事件处理接口的实例, #监听器对象必须通过源对象注册。 例如:实现如图所示程序,设置窗口的背景色为按钮对应颜色。 [[文件:java8-9.png]] 具体实现如下: ===实现窗体布局=== <nowiki>package task10; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class BackGroundColor extends JFrame{ private JButton btRed; private JButton btGreen; private JButton btBlue; private JPanel buttonPanel; public BackGroundColor(){ super("改变颜色"); btRed = new JButton("红色"); btGreen = new JButton("绿色"); btBlue = new JButton("蓝色"); buttonPanel= new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(btRed); buttonPanel.add(btGreen); buttonPanel.add(btBlue); this.add(buttonPanel); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub BackGroundColor bg = new BackGroundColor(); } } </nowiki> ===观察有什么事件需要处理=== 由前面的知识知道,点击按钮所触发的事件是ActionEvent 我们需要处理ActionEvent事件。 ===创建内部类监听器类=== 处理相关事件的方法就是创建一个监听器(类)。 因此,我们: *创建内部类监听器类ComputeAction, *实现接口ActionListener *并实现接口中的方法actionPerformed(ActionEvent e),在方法中添加事件处理内容。 <nowiki>private class ColorAction implements ActionListener{ private Color bgColor; public ColorAction( ){ } public ColorAction(Color bgColor){ this.bgColor = bgColor; } @Override public void actionPerformed(ActionEvent e) { buttonPanel.setBackground(bgColor); } }</nowiki> ===生成事件监听器对象,并为按钮注册监听器对象=== <nowiki>btRed.addActionListener(new ColorAction(Color.RED)); btGreen.addActionListener(new ColorAction(Color.GREEN)); btBlue.addActionListener(new ColorAction(Color.BLUE)); </nowiki> ==完整代码== <nowiki>public class BackGroundColor extends JFrame { private JButton btRed; private JButton btGreen; private JButton btBlue; final JPanel buttonPanel; public BackGroundColor(){ super("改变颜色"); btRed = new JButton("红色"); btGreen = new JButton("绿色"); btBlue = new JButton("蓝色"); btRed.addActionListener(new ColorAction(Color.RED)); btGreen.addActionListener(new ColorAction(Color.GREEN)); btBlue.addActionListener(new ColorAction(Color.BLUE)); buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(btRed); buttonPanel.add(btGreen); buttonPanel.add(btBlue); add(buttonPanel); setSize(300,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { BackGroundColor bg = new BackGroundColor(); } private class ColorAction implements ActionListener{ private Color bgColor; public ColorAction(Color bgColor){ this.bgColor = bgColor; } @Override public void actionPerformed(ActionEvent e) { buttonPanel.setBackground(bgColor); } } } </nowiki> 返回 [[Java程序设计]]
返回至
注册事件监听器和处理事件
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息