A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© MUFCRyanCR7 中级黑马   /  2015-9-17 14:04  /  446 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
        创建图形化界面的步骤:
        1.创建Frame窗体
        2.对窗体进行基本设置
        3.定义组件
        4.用add方法将组件添加到窗体中
        5.通过setVisible(true)让窗体显示
       
       
        事件监听机制:组成:事件源、事件、监听器、事件处理
        1.事件源:awt包或swing包中的图形界面组件
        2.事件:每一个事件源都有自己特有的对应事件和共性事件
        3.监听器:将可以触发某一个事件的动作(动作不止一个)都已经封装到了监听器中
        以上三者,java中已定义好,可直接获取其对象使用,对于事件处理,即对产生的动作的处理方式是我们所需要做的

*/

import java.awt.*;
import java.awt.event.*;                //进行事件处理时一定要导入此包
public class Awt1 {

        public static void main(String[] args)
        {
                Frame frame = new Frame();
                frame.setSize(500, 300);
                frame.setLocation(200, 100);
                frame.setLayout(new FlowLayout());                //设定窗体的布局模式
                Button b1 = new Button("Ryan");
                frame.add(b1);
                //frame.addWindowListener(new MyWin());
                frame.addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent e)
                        {
                                System.out.println("Close");
                                System.exit(0);
                        }
                        public void windowActivated(WindowEvent e)
                        {
                                System.out.println("Active");
                        }
                        public void windowOpened(WindowEvent e)
                        {
                                System.out.println("Open");
                        }
                });
                frame.setVisible(true);       
        }
}

class MyWin extends WindowAdapter
{
        public void windowClosing(WindowEvent e)
        {
                System.out.println("Close");
                System.exit(0);
        }
        public void windowActivated(WindowEvent e)
        {
                System.out.println("Active");
        }
        public void windowOpened(WindowEvent e)
        {
                System.out.println("Open");
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马