美文网首页
接口事件

接口事件

作者: 招风小妖怪 | 来源:发表于2019-07-12 08:41 被阅读0次
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
//2、接口形式做事件
//实现接口
public class Demo06 extends Frame implements ActionListener, WindowListener 
{
    Button btn1 = new Button("按钮1");
    Button btn2 = new Button("按钮2");
    Demo06()
    {
        setSize(500,500);
        setLayout(new FlowLayout());
        
        add(btn1);
        add(btn2);
        //注册事件
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        //给按钮设置特征值
        btn1.setActionCommand("btn1");
        btn2.setActionCommand("btn2");
        //注册事件
        this.addWindowListener(this);
                //显示窗口
        setVisible(true);

    }
    
    
    public static void main(String s[])
    {
        new Demo06();
    }

    public void actionPerformed(ActionEvent parm1) {
        // TODO: Add your code here
        System.out.println("hello  :"+parm1.getActionCommand());
        if(parm1.getActionCommand()=="btn1")
        {
            System.out.println("hello  :按钮1"+parm1.getActionCommand());
        }
        if(parm1.getActionCommand()=="btn2")
        {
            System.out.println("hello  :按钮2"+parm1.getActionCommand());
        }
    }

    public void windowOpened(WindowEvent parm1) {
        // TODO: Add your code here
        System.out.println("窗口已经打开");
    }

    public void windowClosing(WindowEvent parm1) {
        // TODO: Add your code here
        
        System.out.println("窗口已经关闭");
        System.exit(1);
    }

    public void windowClosed(WindowEvent parm1) {
        // TODO: Add your code here
        
    }

    public void windowIconified(WindowEvent parm1) {
        // TODO: Add your code here
    }

    public void windowDeiconified(WindowEvent parm1) {
        // TODO: Add your code here
    }

    public void windowActivated(WindowEvent parm1) {
        // TODO: Add your code here
    }

    public void windowDeactivated(WindowEvent parm1) {
        // TODO: Add your code here
    }
}

相关文章

网友评论

      本文标题:接口事件

      本文链接:https://www.haomeiwen.com/subject/labckctx.html