事件监听机制
1.事件监听机制的流程图:
2.不同种类的事件监听机制;
<1>.以自身的类创建事件监听器:
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
/**
*@ClassName: Listener.java
*@Decription: TODO
*@author LIU
*
*@version: V1.0
*@Date: 2015年9月22日下午9:07:37
*/
public class Listener extends JFrame implements ActionListener {
public Listener(){
super("以本身的类创建事件监听器");
setBounds(200,200,400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button bn = new Button("改变颜色");
bn.setBounds(250, 250, 20, 20);
bn.addActionListener(this);
Container ct = getContentPane();
ct.add(bn);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Container c= getContentPane();
c.setBackground(Color.red);
System.out.println("你的颜色已经改变");
}
public static void main(String[] args){
new Listener();
}
}
<2>.用外部类创建事件监听器:
/**
*
*/
package edu.neuq.GUI;
import java.awt.Button;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
/**
*@ClassName: Listener02.java
*@Decription: TODO
*@author LIU
*
*@version: V1.0
*@Date: 2015年9月22日下午10:00:39
*/
public class Listener02 extends JFrame{
public Listener02(){
super("使用外部类创建事件监听器");
setBounds(200,200,400,400);
setLayout(new FlowLayout());
Button bn = new Button("数值增加");
bn.addActionListener(new myActionListener());
Container c= getContentPane();
c.add(bn);
setVisible(true);
}
public static void main(String[] args){
new Listener02();
}
}
class myActionListener implements ActionListener{
int i = 1;
public void actionPerformed(ActionEvent e) {
System.out.println(i++);
}
}
3.匿名内部类作为事件监听器:
/**
*
*/
package edu.neuq.GUI;
import java.awt.Button;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
/**
*@ClassName: Listener03.java
*@Decription: TODO
*@author LIU
*
*@version: V1.0
*@Date: 2015年9月22日下午10:21:39
*/
public class Listener03 extends JFrame {
public Listener03(){
setSize(500,500);
setLocation(100,100);
setLayout(new FlowLayout());
Button bnt = new Button("数值减少");
bnt.setSize(20, 20);
bnt.setSize(50,50);
bnt.addActionListener(new ActionListener(){
int i = 100;
public void actionPerformed(ActionEvent e) {
System.out.println(i--);
}
});
Container ct = getContentPane();
ct.add(bnt);
setVisible(true);
}
public static void main(String[] args){
new Listener03();
}
}
4.内部类作为事件监听器:
/**
*
*/
package edu.neuq.GUI;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
/**
*@ClassName: Listener04.java
*@Decription: TODO
*@author LIU
*
*@version: V1.0
*@Date: 2015年9月22日下午10:40:37
*/
public class Listener04 extends JFrame{
public Listener04(){
super("内部类作为事件监听器");
setSize(400,400);
setLocation(100,100);
setLayout(new FlowLayout());
Button bnt = new Button("改变颜色");
bnt.addActionListener(new ActionListener01());
bnt.setSize(20,20);
bnt.setLocation(50,50);
Container ct =getContentPane();
ct.add(bnt);
setVisible(true);
}
class ActionListener01 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Container ct = getContentPane();
ct.setBackground(Color.gray);
}
}
public static void main(String[] args){
new Listener04();
}
}