人类要在竞争中求生存,更要奋斗。——孙中山
本讲内容:事件处理
导入包:
import java.awt.event.*;
一、事件分为两大类:低级事件和高级事件。
1、低级事件包括以下几种
事件类别 | 接口名 | 描述 |
ComponentEvent 组件事件 | ComponentListener | 发生于组件大小改变、移动、显示或隐藏时 |
ContainerEvent 容器事件 | ContainerListener | 发生于添加或删除一个组件时 |
WindowEvent 窗口事件 | WindowListener | 窗口被激活、屏蔽、最小最 大化、或关闭 |
MouseEvent 鼠标事件 | MouseEventListener | 鼠标按键被按下、释放、鼠标移动或拖动 |
KeyEvent 键盘事件 | KeyListener | 键盘上的一个键被按下或释放 |
FocusEvent 焦点事件 | FocusEvent | 发生于组件获得焦点或者失去焦点时 |
2、高级事件包括以下几种
事件类别 | 接口名 | 描述 |
ActionEvent 动作事件 | ActionListener | 发生于按钮单击、菜单选择、列表框选择、在文本域中按“回车”等时 |
AdjustmentEvent 调整事件 | AdjustmentListener | 发生于用户在滚动条上移动滑块以调节数值时 |
ItemEvent 项目事件 | ItemListener | 发生于用户从一组选择框或者列表框中进行选择 |
TextEvent 文本事件 | TextListener | 发生于文本域或者文本框中的内容发生改变时 |
例一:两个按钮实现相应事件
public class Text extends JFrame implements ActionListener{
JPanel mb=null;
JButton an1,an2;
public static void main(String[] args) {
Text t=new Text();
}
public Text() {
mb=new JPanel();
mb.setBackground(Color.yellow);
an1=new JButton("红色");
an1.addActionListener(this);//注册监听
an1.setActionCommand("rea");//指定action命令
an2=new JButton("蓝色");
an2.addActionListener(this);
an2.setActionCommand("blue");
this.add(an1,BorderLayout.NORTH);
this.add(an2,BorderLayout.SOUTH);
this.add(mb);
//设置窗体属性
this.setTitle("技术大牛—小劲");
this.setLocation(300, 300);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("rea")){
mb.setBackground(Color.red);
}else if(e.getActionCommand().equals("blue")){
mb.setBackground(Color.blue);
}
}
}
方法二(也可以获得监听事件)
public class Text extends JFrame {
JPanel mb=null;
JButton an1,an2;
public static void main(String[] args) {
Text t=new Text();
}
public Text() {
mb=new JPanel();
mb.setBackground(Color.yellow);
an1=new JButton("红色");
an2=new JButton("蓝色");
MyListener listener=new MyListener();
an1.addActionListener(listener);//注册监听
an1.setActionCommand("red");//指定action命令
an2.addActionListener(listener);
an2.setActionCommand("blue");
this.add(an1,BorderLayout.NORTH);
this.add(an2,BorderLayout.SOUTH);
this.add(mb);
//设置窗体属性
this.setTitle("技术大牛—小劲");
this.setLocation(300, 300);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("red")){
System.out.println("red");
}else if(e.getActionCommand().equals("blue")){
System.out.println("blue");
}
}
}
例二:一个长方形按键盘上下左右会动
public class Text extends JFrame {
MyPanel mb=null;
public static void main(String[] args) {
Text t=new Text();//每定义一个 t 都会产生一个对应的this
}
public Text() {
mb=new MyPanel();
this.add(mb);
this.addKeyListener(mb);//添加监听 键盘监听这个类所定义的对象 用mp对象进行监听
//设置窗体属性
this.setTitle("技术大牛—小劲");
this.setLocation(300, 300);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener{
int x=100,y=100;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
}
//键被按下
public void keyPressed(KeyEvent e) {
// System.out.println("1");
if(e.getKeyCode()==KeyEvent.VK_UP){
y--;
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
y++;
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
x--;
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x++;
}
//调用 repaint() 函数,来重绘制界面
this.repaint();
}
//键被释放
public void keyReleased(KeyEvent e) {
System.out.println("键被释放");
}
<span style="color:#ff0000;">//键的一个值被打印输出(上下左右键等不会执行,因为没打印出)</span>
public void keyTyped(KeyEvent e) {
System.out.println("其它键会执行,上下左右键等不会执行,因为没打印出");
}
}
本讲就到这里,Take your time and enjoy it