zoukankan      html  css  js  c++  java
  • 201671010145 2016-2017《Java程序设计》java事件处理的三种方式

    方法一:使用接口

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class JiSuanQi extends JFrame implements ActionListener{
        JButton jb=new JButton("关闭");
        JiSuanQi(){
            this.setSize(350, 80);
            this.setLocation(300, 300);
            this.setTitle("计算器");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.add(jb);
            jb.addActionListener(this);
        }
        public static void main(String[] args) {
            JiSuanQi j=new JiSuanQi();
            j.setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getSource().equals(jb)){
                System.exit(0);
            }
        }
    }

    方法二:定义一个类,使用接口

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class JiSuanQi extends JFrame{
        JButton jb=new JButton("关闭");
        JiSuanQi(){
            this.setSize(350, 80);
            this.setLocation(300, 300);
            this.setTitle("计算器");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.add(jb);
            jb.addActionListener(new ButtonAction());
        }
        public static void main(String[] args) {
            JiSuanQi j=new JiSuanQi();
            j.setVisible(true);
        }
        class ButtonAction implements ActionListener{
            public void actionPerformed(ActionEvent arg0) {
                if(arg0.getSource().equals(jb)){
                    System.exit(0);
                }
            }
        }
    }

    方法三:使用匿名内部类

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class JiSuanQi extends JFrame{
        JButton jb=new JButton("关闭");
        JiSuanQi(){
            this.setSize(350, 80);
            this.setLocation(300, 300);
            this.setTitle("计算器");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.add(jb);
            jb.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    System.exit(0);
                }
            });
        }
        public static void main(String[] args) {
            JiSuanQi j=new JiSuanQi();
            j.setVisible(true);
        }
    }
  • 相关阅读:
    C#生成MD5的方法
    平常心是道
    Android 三种动画的使用 – Tween Animation
    17个Javascript日期选择器
    Javascript解码编码常用函数
    mysql 命令行导入导出数据
    技术驱动还是产品驱动
    Ubuntu 和 Redhat / Fedora 服务管理命令对比表
    jquery常用技巧
    Fedora 17安装JDK1.7
  • 原文地址:https://www.cnblogs.com/1996zq/p/7860339.html
Copyright © 2011-2022 走看看