事件监听机制



          在java语言中,事件监听器有ActionListener(动作监听器)、MouseListener(鼠标监听器)、MousemotionListener(鼠标动作监听器)、KeyListener(键盘监听器)等等。在源代码中,这些监听器都是接口,因此,要实现这些事件的监听,就必须再新建一个类来继承要使用的接口,新建的类中又必须重写该接口的所有方法,于是事件监听就容易完成了。

      1、ActionListener(动作监听器):在实现此接口的类中,可以给需要关注其动作的组件(如JButton类型的)添加该监听器,之后在事件处理方法(public void actionPerformed(ActionEvent event){})中,对每个事件进行不同处理。比如在一个登陆界面中,你需要添加一个登陆按钮,而要使点击该按钮后有反应,就需要在该扭上添加该监听器,再在事件处理方法中添加方法体就可以实现一定功能。

public class MyUI1 extends JFrame{

private JTextField jtf1;

private JPasswordField jtf2;

public static void main(String[] args) {

new MyUI1().init();

}

public void init(){

this.setTitle("MyQQ");

this.setSize(430,350);

this.getContentPane().setBackground(new Color(235,242,249));

this.setUndecorated(true);//去边框

this.setResizable(false);

EastPanel();

NorthPanel();

WestPanel();

this.setDefaultCloseOperation(3);

this.setLocationRelativeTo(null);

this.setVisible(true);

}

public void NorthPanel(){

JPanel north=new JPanel();

north.setPreferredSize(new Dimension(0,185));

north.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));

//创建一个图标对象

ImageIcon image=new ImageIcon("image/catch.jpg");

 

LoginListener mouse=new LoginListener(this);

 

north.addMouseListener(mouse);

//将图片添加至组件上

JLabel jla=new JLabel(image);

north.add(jla);

ImageIcon image1=new ImageIcon("image/qq11.jpg");

//JButton jbu=new JButton(image1);

//north.add(jbu);

//jbu.setPreferredSize(new Dimension(30,30));

//jbu.setBackground(Color.black);

this.add(north,BorderLayout.NORTH);

}

public void WestPanel(){

JPanel west=new JPanel();

west.setPreferredSize(new Dimension(131,0));

west.setBackground(new Color(235,242,249));

west.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));

//创建一个图标对象

ImageIcon image=new ImageIcon("image/catch2.jpg");

//将图片添加至组件上

JLabel jla=new JLabel(image);

west.add(jla);

this.add(west,BorderLayout.WEST);

}

public void EastPanel(){

JPanel east=new JPanel();

east.setBackground(new Color(235,242,249));

east.setPreferredSize(new Dimension(300,0));

east.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));

jtf1=new JTextField(17);

jtf1.setPreferredSize(new Dimension(193,28));

jtf2=new JPasswordField(17);

jtf2.setPreferredSize(new Dimension(193,28));

//jtf1.setFont(new Font("楷体",4,18));

east.add(jtf1);

ImageIcon image=new ImageIcon("image/qq4.jpg");

JLabel jl1=new JLabel(image);

east.add(jl1);

east.add(jtf2);

ImageIcon image2=new ImageIcon("image/qq3.jpg");

JLabel jl2=new JLabel(image2);

east.add(jl2);

ImageIcon image1=new ImageIcon("image/qq1.jpg");

JLabel jl3=new JLabel(image1);

east.add(jl3);

ImageIcon image3=new ImageIcon("image/qq2.jpg");

JButton jbu=new JButton(image3);

jbu.setPreferredSize(new Dimension(192,30));

east.add(jbu);

MyUI2 listener=new MyUI2(jtf1, jtf2);

jbu.addActionListener(listener);

ImageIcon image4=new ImageIcon("image/qq5.jpg");

JLabel jl4=new JLabel(image4);

east.add(jl4);

this.add(east,BorderLayout.EAST);

}

}


 

 该代码实现的功能就是一个登陆界面,只是按钮上加的是图片,登陆按钮上加的就是该监听器,点击按钮后就会执行事件监听方法。

public class MyUI2 extends JFrame implements ActionListener{

private JTextField jtf1;

private JPasswordField jtf2;

private Mouse listener2;

ButtonListener listener1;

//private Mouse listener2;

public MyUI2(JTextField jtf1,JPasswordField jtf2){

this.jtf1=jtf1;

this.jtf2=jtf2;

}

public void actionPerformed(ActionEvent e) {

if(jtf1.getText().equals("")&&jtf2.getText().equals("")){

this.setTitle("画图板");

this.setSize(new Dimension(800,700));

this.setDefaultCloseOperation(3);

this.setLocationRelativeTo(null);

this.setResizable(false);

listener1=new ButtonListener(this);

this.WestPanel();

this.NorthPanel();

this.EastPanel();

this.setVisible(true);

Graphics g=this.getGraphics();

listener2=new Mouse(g,listener1);

listener1.setLis(listener2);

this.addMouseListener(listener2);

this.addMouseMotionListener(listener2);

 

}else

JOptionPane.showConfirmDialog(null, "您输入的密码有误!");

}

public void NorthPanel(){

JPanel north=new JPanel();

north.setPreferredSize(new Dimension(0,80));

//north.setBackground(Color.black);

north.setBackground(new Color(200,20,180));

//north.setOpaque(false);

JLabel jbl=new JLabel("画     图     板");

north.add(jbl);

jbl.setFont(new Font("隶书",4,60));

this.add(north,BorderLayout.NORTH);

}

public void EastPanel(){

JPanel east=new JPanel();

east.setPreferredSize(new Dimension(95,0));

east.setBackground(new Color(70,250,250));

east.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));

String lis[]={"橡皮","清除","重画"};

for(int i=0;i<lis.length;i++){

JButton jbu2=new JButton(lis[i]+"");

east.add(jbu2);

jbu2.addActionListener(listener1);

jbu2.setBackground(new Color(155,155,155));

jbu2.setForeground(new Color(255,255,255));

jbu2.setPreferredSize(new Dimension(60,30));

}

String str[]={"画笔","直线","圆","椭圆","六角星","矩形","彩线","球","彩球"};

for(int i=0;i<str.length;i++){

JButton jbu=new JButton(str[i]+"");

east.add(jbu);

jbu.setBackground(new Color(240,20,20));

jbu.setForeground(new Color(255,255,255));

jbu.setPreferredSize(new Dimension(90,47));

jbu.addActionListener(listener1);

}

this.add(east,BorderLayout.EAST);

}

public void WestPanel(){

JPanel west=new JPanel();

west.setPreferredSize(new Dimension(150,0));

west.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5));

west.add(new JLabel("颜色1:"));

west.setBackground(new Color(70,250,250));

for(int i=0;i<15;i++){

JButton jbu=new JButton();

jbu.setPreferredSize(new Dimension(30,30));

west.add(jbu);

jbu.setBackground(new Color(3*i,4*i,5*i));

}

west.add(new JLabel("颜色2:"));

for(int i=0;i<15;i++){

JButton jbu=new JButton();

jbu.setPreferredSize(new Dimension(30,30));

west.add(jbu);

jbu.setBackground(new Color(9*i,5*i+20,8*i+70));

}

west.add(new JLabel("颜色3:"));

for(int i=0;i<15;i++){

JButton jbu=new JButton();

jbu.setPreferredSize(new Dimension(30,30));

west.add(jbu);

jbu.setBackground(new Color(8*(15-i)+100,3*i+10,2*i));

}

west.add(new JLabel("颜色4:"));

for(int i=0;i<15;i++){

JButton jbu=new JButton();

jbu.setPreferredSize(new Dimension(30,30));

west.add(jbu);

jbu.setBackground(new Color(255-3*i,255-4*i,255-3*i));

}

this.add(west,BorderLayout.WEST);

}

public void paint(Graphics g){

super.paint(g);

if(listener2.array.array!=null){

for(int i=0;i<listener2.array.array.length;i++){

Shapes shape=listener2.array.array[i];

int x1=shape.x1;int x2=shape.x2;int y1=shape.y1;int y2=shape.y2;

if(shape.str.equals("直线")){

g.drawLine(x1,y1,x2,y2);

}

if(shape.str.equals("圆")){

g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs((x2-x1+y2-y1)/2),Math.abs((x2-x1+y2-y1)/2));

}

if(shape.str.equals("椭圆")){

g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));

}

if(shape.str.equals("矩形")){

g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));

}

if(shape.str.equals("球")){

for(int j=0;j<100;j++){

g.setColor(new Color(j,j*2+50,j+100));

g.fillOval(x1+j/2-50,y1+j/2-50,100-j,100-j);

}

}

if(shape.str.equals("六角星")){

g.drawLine((x1+x2)/2, y1, (x2+2*x1)/3, (y2+3*y1)/4);

g.drawLine((x1+x2)/2, y1, (2*x2+x1)/3, (y2+3*y1)/4);

g.drawLine((x2+2*x1)/3, (y2+3*y1)/4, x1,(y2+3*y1)/4 );

g.drawLine(x1, (y2+3*y1)/4, (x2+5*x1)/6, (y1+y2)/2);

g.drawLine((x2+5*x1)/6, (y1+y2)/2, x1, (3*y2+y1)/4);

g.drawLine(x1, (3*y2+y1)/4, (x2+2*x1)/3, (3*y2+y1)/4);

g.drawLine( (x2+2*x1)/3,(3*y2+y1)/4, (x1+x2)/2, y2);

g.drawLine((x1+x2)/2, y2, (2*x2+x1)/3, (3*y2+y1)/4);

g.drawLine((2*x2+x1)/3, (3*y2+y1)/4, x2, (3*y2+y1)/4);

g.drawLine(x2, (3*y2+y1)/4, (5*x2+x1)/6, (y2+y1)/2);

g.drawLine((5*x2+x1)/6,(y2+y1)/2, x2, (y2+3*y1)/4);

g.drawLine(x2, (y2+3*y1)/4,(2*x2+x1)/3,(y2+3*y1)/4);

}

if(shape.str.equals("彩球")){

for(int j=0;j<50;j++){

g.setColor(new Color(2*j+155,2*j,2*j+155));

g.fillOval(x1+j-50,y1+j-50,100-2*j,100-2*j);

}

}

if(shape.str.equals("彩线")){

g.setColor(new Color(x2%255,y2%255,x2%255));

g.drawLine(x1,y1,x2,y2);

}

if(shape.str.equals("画笔")){

g.drawLine(x1,y1, x2, y2);

x1=x2;

y1=y2;

}

if(shape.str.equals("橡皮")){

g.setColor(new Color(238,238,238));

g.fillOval(x1, y1, 25,25);

}

}

}

}

}



 

该代码就是继承该接口的类,点击登录按钮后就会出现一个画图板窗体。

      2、MouseListener(鼠标监听器):该监听器可以加在窗体上或Jpanel等组件上。上面一段代码中的窗体上就加了该监听器。

public class Mouse implements MouseListener,MouseMotionListener{

private String str;

Array array=new Array();

private int x1,x2,y1,y2;

private ButtonListener listener;

private Graphics g;

public Mouse(Graphics g,ButtonListener listener){

this.g=g;

this.listener=listener;

}

public Mouse(){

 

}

 

public void mouseClicked(MouseEvent e) {

 

}

 

@Override

public void mousePressed(MouseEvent e) {

x1=e.getX();

y1=e.getY();

 

 

 

}

 

@Override

public void mouseReleased(MouseEvent e) {

x2=e.getX();

y2=e.getY();

 

if(listener.getSTR().equals("圆")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs((x2-x1+y2-y1)/2),Math.abs((x2-x1+y2-y1)/2));

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="圆";

array.add(shape);

 

}

else if(listener.getSTR().equals("直线")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

Graphics g1=(Graphics)g;

g1.drawLine(x1,y1,x2,y2);

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="直线";

array.add(shape);

}

else if(listener.getSTR().equals("椭圆")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="椭圆";

array.add(shape);

}else if(listener.getSTR().equals("矩形")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="矩形";

array.add(shape);

}else if(listener.getSTR().equals("球")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

Color co=g.getColor();

for(int i=0;i<100;i++){

g.setColor(new Color(i,i*2+50,i+100));

g.fillOval(x1+i/2-50,y1+i/2-50,100-i,100-i);

}

Shapes shape=new Shapes();

g.setColor(co);

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="球";

array.add(shape);

}else if(listener.getSTR().equals("六角星")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

g.drawLine((x1+x2)/2, y1, (x2+2*x1)/3, (y2+3*y1)/4);

g.drawLine((x1+x2)/2, y1, (2*x2+x1)/3, (y2+3*y1)/4);

g.drawLine((x2+2*x1)/3, (y2+3*y1)/4, x1,(y2+3*y1)/4 );

g.drawLine(x1, (y2+3*y1)/4, (x2+5*x1)/6, (y1+y2)/2);

g.drawLine((x2+5*x1)/6, (y1+y2)/2, x1, (3*y2+y1)/4);

g.drawLine(x1, (3*y2+y1)/4, (x2+2*x1)/3, (3*y2+y1)/4);

g.drawLine( (x2+2*x1)/3,(3*y2+y1)/4, (x1+x2)/2, y2);

g.drawLine((x1+x2)/2, y2, (2*x2+x1)/3, (3*y2+y1)/4);

g.drawLine((2*x2+x1)/3, (3*y2+y1)/4, x2, (3*y2+y1)/4);

g.drawLine(x2, (3*y2+y1)/4, (5*x2+x1)/6, (y2+y1)/2);

g.drawLine((5*x2+x1)/6,(y2+y1)/2, x2, (y2+3*y1)/4);

g.drawLine(x2, (y2+3*y1)/4,(2*x2+x1)/3,(y2+3*y1)/4);

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="六角星";

array.add(shape);

}else if(listener.getSTR().equals("彩球")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

Color co=g.getColor();

for(int i=0;i<50;i++){

g.setColor(new Color(2*i+155,2*i,2*i+155));

g.fillOval(x1+i-50,y1+i-50,100-2*i,100-2*i);

}

g.setColor(co);

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="彩球";

array.add(shape);

 

}

}

@Override

public void mouseEntered(MouseEvent e) {

 

}

 

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

 

}

@Override

public void mouseDragged(MouseEvent e) {

x2=e.getX();

y2=e.getY();

if(listener.getSTR().equals("彩线")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

Color co=g.getColor();

g.setColor(new Color(x2%255,y2%255,x2%255));

g.drawLine(x1, y1, x2, y2);

g.setColor(co);

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="彩线";

array.add(shape);

}else if(listener.getSTR().equals("画笔")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

g.drawLine(x1, y1, x2, y2);

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="画笔";

array.add(shape);

x1=x2;

y1=y2;

}else if(listener.getSTR().equals("橡皮")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){

Color co=g.getColor();

g.setColor(new Color(238,238,238));

g.fillOval(x1, y1, 25,25);

g.setColor(co);

x1=x2;

y1=y2;

Shapes shape=new Shapes();

shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="橡皮";

array.add(shape);

}

}

@Override

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

}

 

}

这就是继承该接口的类,这样就可以根据鼠标的动作来实现其中的方法了。

  MousemotionListener(鼠标动作监听器)以及KeyListener(键盘监听器)的监听机制基本差不多,就不一一举例了。总之,事件监听功能非常强大,可以根据需要来选择要实现哪个监听机制


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值