GUI

来源:互联网 发布:centos 6.9 安全设置 编辑:程序博客网 时间:2024/05/06 00:46

Frame

//创建图形化界面://1,创建frame窗体。//2,对窗体进行基本设置。    比如大小,位置,布局。//3,定义组件。//4,将组件通过窗体的add方法添加到窗体中。//5,让窗体显示,通过setVisible(true)    public static void main(String[] args)     {    Frame f = new Frame("my awt");    //f.setBounds(x, y, width, height)//设置坐标大小        f.setSize(500,400);//设置大小        f.setLocation(300,200);//设置坐标        f.setLayout(new FlowLayout());//设置布局管理器        Button b = new Button("我是一个按钮");        f.add(b);//添加按钮

事件监听机制的特点:
1,事件源。
2,事件。Event
3,监听器。Listener
4,事件处理。

事件源:就是awt包或者swing包中的那些图形界面组件。

事件:每一个事件源都有自己特有的对应事件和共性事件。

监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。

class MyWin implements WindowListener{    //覆盖7个方法。可以我只用到了关闭的动作。    //其他动作都没有用到,可是却必须复写。}//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。//并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。//import java.awt.*; import java.awt.event.*;需要导入这两个包//关闭窗口事件    f.addWindowListener(new WindowAdapter(){    public void windowClosing(WindowEvent e)            {                System.out.println("我关");                System.exit(0);            }//另一种方法class MyWin extends WindowAdapter{    public void windowClosing(WindowEvent e)    {        //System.out.println("window closing---"+e.toString());        System.exit(0);    }}

Frame分离操作

class FrameDemo {    // 定义该图形中所需的组件的引用。    private Frame f;    private Button but;    FrameDemo() {        init();    }    public void init() {        f = new Frame("my frame");        // 1、对frame进行基本设置。        f.setBounds(300, 100, 600, 500);        f.setLayout(new FlowLayout());        but = new Button("my button");        //2、 将组件添加到frame中        f.add(but);        // 4、加载一下窗体上事件。        myEvent();        //3、 显示窗体;        f.setVisible(true);    }//------------------------------------------------------------------------------------------------    private void myEvent() {        f.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        // 让按钮具备退出程序的功能        /*         * 按钮就是事件源。 那么选择哪个监听器呢? 通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。 需要查看该组件对象的功能。         * 通过查阅button的描述。发现按钮支持一个特有监听addActionListener。         */        but.addActionListener(new ActionListener() {            private int count = 1;//少数没有适配器的监听器,因为Api方法只有一个            public void actionPerformed(ActionEvent e) {                // System.out.println("退出,按钮干的");                System.exit(0);                // f.add(new Button("Button-"+(count++)));                // f.setVisible(true);                // f.validate();                // System.out.println(e.getSource());                Button b = (Button) e.getSource();                Frame f1 = (Frame) b.getParent();                f1.add(new Button("button-" + count++));                f1.validate();            }        });    }//------------------------------------------------------------------------------------------------    public static void main(String[] args) {        new FrameDemo();    }}

鼠标键盘文本事件

import java.awt.*;import java.awt.event.*;class MouseAndKeyEvent {    private Frame f;    private Button but;    private TextField tf;    MouseAndKeyEvent()    {        init();    }    public void init()    {        f = new Frame("my frame");        f.setBounds(300,100,600,500);        f.setLayout(new FlowLayout());        tf = new TextField(20);        but = new Button("my button");          f.add(tf);        f.add(but);        myEvent();        f.setVisible(true);    }    private void myEvent()    {        f.addWindowListener(new WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        });//---------------------------------------------------------------------------------------------        //给文本添加一个键盘监听。        tf.addKeyListener(new KeyAdapter()        {            public void keyPressed(KeyEvent e)            {                int code = e.getKeyCode();                if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))                {                    System.out.println(code+".....是非法的");                    e.consume();                }            }        });        //给But添加一个键盘监听。        but.addKeyListener(new KeyAdapter()        {            public void keyPressed(KeyEvent e)            {                   //按exit退出                if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)                    //System.exit(0);                System.out.println("ctrl+enter is run");                //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());            }        });//-----------------------------------------------------------------------------------------//活动监听              but.addActionListener(new ActionListener()        {            public void actionPerformed(ActionEvent e)            {                System.out.println("action ok");            }        });//----------------------------------------------------------------------------------------------                //鼠标进入事件,鼠标放到按钮就触发        but.addMouseListener(new MouseAdapter()        {            private int count = 1;            private int clickCount = 1;            public void mouseEntered(MouseEvent e)             {                System.out.println("鼠标进入到该组件"+count++);            }            public void mouseClicked(MouseEvent e)            {                if(e.getClickCount()==2)                    System.out.println("双击动作"+clickCount++);//             }        });    }//-----------------------------------------------------------------------------------------------    public static void main(String[] args)     {        new MouseAndKeyEvent();    }}
0 0
原创粉丝点击