事件监听

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

//监听者,接口监听类
public class Listener extends JFrame implements ActionListener{
    //定义一个Panel
    JPanel mp=null;
    JButton jb1=null;
    JButton jb2=null;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Listener lt=new Listener();
    }
    
    //构造函数,初始化类变量
    public Listener(){
        mp=new JPanel();
        jb1=new JButton("黑色");
        jb2=new JButton("红色");
        mp.setBackground(Color.blue);
        
        
        
        this.add(jb1,BorderLayout.NORTH);
        this.add(mp);
        this.add(jb2,BorderLayout.SOUTH);
        
        
        
        //注册监听 (this是监听类去监听)
        //事件源是jb1 jb2,监听对象是this
        jb1.addActionListener(this);
        jb1.setActionCommand("黑色");
        jb2.addActionListener(this);
        jb2.setActionCommand("红色");
        
        
        this.setTitle("事件处理");
        this.setSize(500, 400);
        this.setLocation(500, 400);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    //事件处理类,对事件的处理方法
    @Override
    public void actionPerformed(ActionEvent e) {
        //System.out.println("被按下按钮");
        //判断哪个键被点击
        if(e.getActionCommand().equals("黑色")){
            mp.setBackground(Color.black);
        }else if(e.getActionCommand().equals("红色")){
            mp.setBackground(Color.red);
        }else{
            System.out.println("没有点击按钮");
        }
    }
    

}

class dog extends JFrame implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("黑色")){
            System.out.println("dog 知道 panel变成黑色");
        }else if(e.getActionCommand().equals("红色")){
            System.out.println("dog 知道 panel变成红色");
        }else{
            System.out.println("dog 啥都不知道");
        }
    }
    
}

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

//监听者,接口监听类
public class Listener extends JFrame implements ActionListener{
    //定义一个Panel
    JPanel mp=null;
    JButton jb1=null;
    JButton jb2=null;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Listener lt=new Listener();
    }
    
    //构造函数,初始化类变量
    public Listener(){
        mp=new JPanel();
        jb1=new JButton("黑色");
        jb2=new JButton("红色");
        mp.setBackground(Color.blue);
        
        
        
        this.add(jb1,BorderLayout.NORTH);
        this.add(mp);
        this.add(jb2,BorderLayout.SOUTH);
        
        
        
        //注册监听 (this是监听类去监听)
        //事件源是jb1 jb2,监听对象是this
        jb1.addActionListener(this);
        jb1.setActionCommand("黑色");
        jb2.addActionListener(this);
        jb2.setActionCommand("红色");
        
        
        this.setTitle("事件处理");
        this.setSize(500, 400);
        this.setLocation(500, 400);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    //事件处理类,对事件的处理方法
    @Override
    public void actionPerformed(ActionEvent e) {
        //System.out.println("被按下按钮");
        //判断哪个键被点击
        if(e.getActionCommand().equals("黑色")){
            mp.setBackground(Color.black);
        }else if(e.getActionCommand().equals("红色")){
            mp.setBackground(Color.red);
        }else{
            System.out.println("没有点击按钮");
        }
    }
    

}

class dog extends JFrame implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getActionCommand().equals("黑色")){
            System.out.println("dog 知道 panel变成黑色");
        }else if(e.getActionCommand().equals("红色")){
            System.out.println("dog 知道 panel变成红色");
        }else{
            System.out.println("dog 啥都不知道");
        }
    }
    
}














原文地址:https://www.cnblogs.com/tsinghuaxiaobao/p/5727255.html