java之初学事件处理- 事件发生器

[color=blue]import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest {
public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame{
public ButtonFrame(){
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class ButtonPanel extends JPanel{
public ButtonPanel(){
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = 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);
}
private class ColorAction implements ActionListener{
public ColorAction(Color c){
backgroundColor = c;
}
public void actionPerformed(ActionEvent event){
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}


为了实现ActionListener接口,监听类必须有一个称为actionPerformed的方法,actionPerformed方法是
ActionListener中的唯一方法,该方法接收一个ActionEvent对象,此对象包含了事件发生时的相关信息
----------------------------------------------------------

***********************javax.swing.JButton

JButton(String label)

参数:label 显示按钮上的文本

***********************java.awt.Container

Component add(Component c)

将组件c添加到容器中

******************java.swing.ImageIcon

imageIcon(String filename)

---------------------------------------------------------
================建立内部类

创造一个图标,存储在一个文件中,通过媒体跟踪器自动加载这个图像

从上边可以看到:每一个按钮的处理过程都是一样的

1.用标签字符串构造按钮

2.将按钮添加到面板

3.用适当的颜色构造一个动作监听器

4.添加动作监听器

为了简化 ,可以使用一个辅助方法:

void makeButton(String name ,Color backgroundColor){
JButton button = new JButton(name);
add(button);
ColorAction action = new JButton (backgroundColor);
button.addActionListener(action);
}

然后将ButtonPanel 的构造器简化为:
public ButtonPanel(){
makeButton(“yellow”,ColorYELLOW);
...
}

紧接着可以进一步简化,注意:ColorAction类只在makeButton方法中用到一次,因此可以设置成一个匿名类

void makeButton(String name ,Color backgroundColor){
JButton button = new JButton(name);
add(button);
button.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent event){
setBackground(backgroundColor);
}
});
}


------------------------------------------------------

============将组件变成事件监听器

任何实现了ActionListener接口的类对象都可以作为按钮监听器


Class ButtonPanel extends JPanel implements ActionListener{
..
public void actionPerformed(ActionEvent event)
{
}
..
}
然后将面板设为三个按钮的监听对象:
yellowButton。addActionListener(this);
..
..
..
现在这三个按钮不再是独立的监听器,他们共享一个监听器,即按钮面板

actionPerformed方法必须判断点击了哪个 按钮

Object source = event.getSource();

actionPerformed方法可以判断哪个按钮是事件源

if(source==yellowButton)
else
..

****************************java.util.EventObject

Object getSource();
返回事件的对象引用

****************java.awt.event.ActionEvent

String getActionCommand()

返回与这个动作事件相关的命令字符串

********************javax.swing.UIManager.LookAndFeelInfo

String getName()

返回观感的显示名称

String getClassName()[/color]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值