【Java】【事件处理机制】

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
栗子 事件监听了解
*/

class Cat implements ActionListener{
public void actionPerformed(ActionEvent arg0){
if(arg0.getActionCommand().equals("黑色")){
System.out.println("Cat也知道你按下了黑色按钮");
}
else if(arg0.getActionCommand().equals("红色")){
System.out.println("Cat也知道你按下了红色按钮");
}
else{
System.out.println("Cat什么也不知道");
}
}
}
public class Test_Action extends JFrame implements ActionListener {
//定义组件
JPanel mp = null;
JButton jb1,jb2;

public Test_Action(){
mp = new JPanel();
jb1 = new JButton("黑色");
jb2 = new JButton("红色");
//设定布局管理器

//加入组件
mp.setBackground(Color.black);
this.add(mp);
this.add(jb1,BorderLayout.NORTH);
this.add(jb2,BorderLayout.SOUTH);

//猫类在监听
Cat mycat1 = new Cat();
jb1.addActionListener(mycat1);
jb2.addActionListener(mycat1);
//注册监听
jb1.addActionListener(this);
jb2.addActionListener(this);

//指定action命令
jb1.setActionCommand("黑色");
jb2.setActionCommand("红色");

//窗体设置
this.setTitle("事件处理机制");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//对事件处理的方法
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("黑色")){
System.out.println("点击了黑色按钮");
}else if(e.getActionCommand().equals("红色")){
System.out.println("点击了红色按钮");
}else{
System.out.println("不知道");
}
}


public static void main(String[] args){
Test_Action ta = new Test_Action();
}



}








import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/*
栗子 加深对事件处理机制的理解
1. 通过上下左右键,来控制一个小球的移动
*/

//定义自己的面板
class MyPanels extends JPanel implements KeyListener{
int x = 10,y = 10;
public void paint(Graphics g){
super.paint(g);
g.fillOval(x,y,10,10);
}

//按键被按下
public void keyPressed(KeyEvent e){
System.out.println("被按下" + ((char)e.getKeyCode()));
if(e.getKeyCode() == KeyEvent.VK_DOWN){
System.out.println("向下");
y++;
}else if(e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("向上");
y--;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
System.out.println("向左");
x--;
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
System.out.println("向右");
x++;
}

//调用repaint(),重绘界面
this.repaint();

}

//按键被弹起
public void keyReleased(KeyEvent e){

}

//keyTyped代表具体的值被输出
public void keyTyped(KeyEvent e){

}
}
public class Test_Action extends JFrame {
//定义组件
MyPanels mp = null;

public Test_Action(){
mp = new MyPanels();

//监听
this.addKeyListener(mp);

//加入组件
this.add(mp);
//设置窗体
this.setTitle("键盘事件监听");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args){
Test_Action ta = new Test_Action();
}

}

按上下左右键可相应的上下左右

 

Java事件处理机制--深入理解

几个重要的概念:1. 事件源 2. 事件 3. 事件监听器

1. 事件源是一个产生(或触发)事件的对象,比如前面的JButton的一个对象jb1. 当这个事件源对象的某些状态以某种方式发生变化时,就会产生某种类型的事件(一个事件源可能会生成多个不同的事件)。如果某个组件(对象)希望得到事件源产生的事件,就需要在这个事件源上注册。

2.事件就是承载事件源状态改变时的信息对象。Java.awt.event 包和 javax.swing.event包中定义了各种事件类型,常见的事件类有:

   ActionEvent        按下按钮,或双击一个列表项或选中某个菜单时

  AdjustmentEvent 当操作一个滚动条

 ComponentEvent  当一个组件隐藏、移动、改变大小时

 ContainerEvent    当一个组件从容器中加入或者删除时

 FocusEvent 当一个组件获得或失去焦点的时候

ItemEvent  当一个复选框或是列表项被选中,当一个选择框或选择菜单被选中

KeyEvent  从键盘的按键被按下,松开时发生

MouseEvent  鼠标被拖动、移动、点击、按下....

TextEvent  文本区和文本域的文本发生改变时

WindowEvent  当一个窗口激活、关闭、失效、恢复、最小化...

 

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*
栗子 单事件源、多事件监听、多事件处理
*/

//1. 让MyPanel知道鼠标按下的消息,并且知道点击的位置(x,y)
//2. 让MyPanel知道哪个键按下了

class MyPanel extends JPanel implements MouseListener,MouseMotionListener,KeyListener,WindowListener{
public void paint(Graphics g){
super.paint(g);
}

//鼠标点击
public void mouseClicked(MouseEvent e){
System.out.println("鼠标点击了x=" + e.getX() + "y=" + e.getY());
}

//鼠标移动到MyPanel(mouseEntered)
public void mouseEntered(MouseEvent e){
System.out.println("鼠标移动到MyPanel上了");
}

//鼠标离开
public void mouseExited(MouseEvent e){
System.out.println("鼠标离开MyPanel了");
}

//鼠标按下去
public void mousePressed(MouseEvent e){
System.out.println("鼠标在MyPanel被按下了");
}

//鼠标松开
public void mouseReleased(MouseEvent e){
System.out.println("鼠标在MyPanel被松开了");
}

//鼠标拖拽
public void mouseDragged(MouseEvent e){
System.out.println("鼠标拖拽x=" + e.getX() + "y=" + e.getY());
}

//鼠标移动
public void mouseMoved(MouseEvent e){
System.out.println("鼠标移动时X=" + e.getX() + "Y=" + e.getY());
}

//键输入值,F(1-12)无响应
public void keyTyped(KeyEvent e){
System.out.println("输入了" + e.getKeyChar() + "键");
}

//键按下
public void keyPressed(KeyEvent e){
System.out.println("按下了" + e.getKeyChar() + "键");
}

//键松开
public void keyReleased(KeyEvent e){
System.out.println("松开了" + e.getKeyChar() + "键");
}

//打开窗口
public void windowOpened(WindowEvent e){
System.out.println("打开窗口了");
}

//窗口关闭Closing
public void windowClosing(WindowEvent e){
System.out.println("窗口关闭Closing");
}

//窗口关闭closed
public void windowClosed(WindowEvent e){
System.out.println("窗口关闭closed");
}

//窗口最小化
public void windowIconified(WindowEvent e){
System.out.println("窗口最小化");
}

//恢复窗口
public void windowDeiconified(WindowEvent e){
System.out.println("恢复窗口");
}

//激活窗口,使用窗口
public void windowActivated(WindowEvent e){
System.out.println("激活窗口");
}

//窗口停用,切换窗口
public void windowDeactivated(WindowEvent e){
System.out.println("窗口停用");
}

}

public class Test_Action extends JFrame {
//定义组件
MyPanel mp = null;


public Test_Action(){
mp = new MyPanel();

//注册监听
this.addMouseListener(mp);
this.addMouseMotionListener(mp);
this.addKeyListener(mp);
this.addWindowListener(mp);

//加入组件
this.add(mp);
//设置窗体
this.setTitle("事件多监听多处理");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){
Test_Action ta = new Test_Action();
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



















posted @ 2018-02-11 16:26  素人渔芙2017  阅读(235)  评论(0编辑  收藏  举报