zoukankan      html  css  js  c++  java
  • 事件监听

    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 啥都不知道");
            }
        }
        
    }














  • 相关阅读:
    [noip2010]关押罪犯 并查集
    双栈排序 noip2008
    欧拉函数
    中国剩余定理(孙子定理)
    求组合数
    线性素数筛
    洛谷 P2661 信息传递
    图的最短路
    Mzc和男家丁的游戏
    最佳旅游线路
  • 原文地址:https://www.cnblogs.com/tsinghuaxiaobao/p/5727255.html
Copyright © 2011-2022 走看看