黑马程序员技术交流社区

标题: 创建图形化界面的步骤 [打印本页]

作者: MUFCRyanCR7    时间: 2015-9-17 14:04
标题: 创建图形化界面的步骤
/*
        创建图形化界面的步骤:
        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");
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2