java之初学事件处理- 事件发生器
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()