zoukankan      html  css  js  c++  java
  • GUI

    GUI组件可以分为两类:基本组件和容器。容器是一种特殊的组件。

    程序的GUI线程由AWT线程管理,并不是主线程结束窗口就会消失。在多线程中如果有一个前台线程没有结束,改程序就不可能结束。

    1. ATW时间处理:

         1.1、 事件处理机制:  事件:用户对组件的一个操作,称之为一个事件。

                                       事件源:发生事件的组件就是事件源。

                                       事件处理器:某个java类中的负责处理事件的成员方法。

     

          一个事件监听器对象负责处理一类事件。 一类事件的每一种发生情况,分别由事件监听器对象中的一个方法来具体处理。  在事件源和事件监听器对象中进行约定的接口类,被称为事件监听器接口。  事件监听器接口类的名称与事件类的名称是对应的,例如:MouseEvent事件类的监听接口名为MouseListener。

         处理发生在某个GUI组件上的XxxEvent事件的某种情况,其事件处理的通用程序编写流程:

          a. 编写一个实现了XxxListener接口的事件监听器类。

          b. XxxListener类中的用于处理该事件情况的方法中,编写处理代码。

          c. 调用组件的addXxxListener方法,将类XxxListener创建的实力对象注册到GUI组件上。

      

    class YourWindowListern extends WindowAdapter{
        //由于适配器中的方法太简单,不能满足业务的需求所以要对对应的处理方法进行覆盖
        @Override
        public void windowClosing(WindowEvent e) { 
            super.windowClosing(e);
            e.getWindow().dispose();
            System.exit(0);
            /*
             * 如果一个方法不是自己调用,而是由java内部自己调用,如果出现与结果不一致的情况,要查看此方法是否被调用
             * */
        }
    }
    class MyWindownListern implements WindowListener{
        @Override public void windowOpened(WindowEvent e) {}
        @Override
        public void windowClosing(WindowEvent e) {
            e.getWindow().setVisible(false);  //区别两个get方法
            ((Window) e.getComponent()).dispose();
            //e.get 得到发生事件的事件源
            System.exit(0);
        }
        @Override public void windowClosed(WindowEvent e) {}
        @Override public void windowIconified(WindowEvent e) {}
        @Override public void windowDeiconified(WindowEvent e) {}
        @Override public void windowActivated(WindowEvent e) {}
        @Override public void windowDeactivated(WindowEvent e) {}
    }
    class ButtonClose implements ActionListener{
        private Frame f = null;
        public ButtonClose(Frame f) {
            this.f = f;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            this.f.dispose();
            //System.exit(0);
        }
        
    }
    public class TestFrame {
        public static void main(String[] args) {
            /*Frame f = new Frame("www.cjt.org");
            f.add(new Button("ok"));
            f.setSize(300, 300);
            f.setVisible(true);
        //    f.addWindowListener(new MyWindownListern());  //将事件注册到f对象上
            f.addWindowListener(new YourWindowListern());*/
            /*    try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            f.dispose(); //释放窗口对象
    */        //f.setVisible(false);  //设置不可见但f对象仍然在内存对象中
            
            //3. ButtonClose
            Frame f = new Frame("www.cjt.org");
            f.setSize(300, 200);
            Button b = new Button("close");
            f.add(b);
            f.setVisible(true);
            b.addActionListener(new ButtonClose(f));
        }
    }

       1.2、事件分类: 按产生事件的物理操作盒GUI组件的表现效果进行分型:  MouseEvent, WindowEvent, ActionEvent ......。

       1.3  事件适配器:  事件适配器类:事件监听器接口的最简单实现类。 用事件适配器来处理事件,可以简化事件监听器的编写。

       1.4 灵活设计事件监听器类: 如果在事件监听器类中要访问非事件源的其他GUI组件,程序该如何编写。(eg:单机button让窗口关闭)

    public class TestFrame implements ActionListener{
        private Frame f = new Frame("www.cjt.org"+i);
        public static int i = 0;
        public TestFrame(){
            i++;
        }
        public void init(){
            Button b = new Button("close");
            f.setSize(300, 200);
            f.add(b);
            f.setVisible(true);
            b.addActionListener(this/*new TestFrame()*/);
    }//面向对象编程思想:脑海中是内存中的一个个对象,通过调用方法之后对象状态发生某种变化
    public static void main(String[] args) { TestFrame t = new TestFrame(); t.init(); } @Override public void actionPerformed(ActionEvent e) { System.out.println("***"+i); f.dispose(); //System.exit(0); } }

    1.4 事件处理的多重运用:

    用户的一个操作在触发了低级事件的同时可能也会处罚语义(高级)事件,此时我们就需要合理的选择要处理的事件监听器类型。 eg:用鼠标点击按钮,既触发了鼠标事件也触发了按钮本身所代表的事件,程序应该根据业务的需要选择对事件进行处理,或者两个都进行处理。如果按钮被点击后程序除了要执行按钮所对应功能外,还希望鼠标按下之后按钮的标题改变鼠标释放时候又恢复,这样就需要注册两个事件处理器。一般来说只需要对语义计算进行处理,而不需要对引发该语义事件的低级事件。

      1.5  修改组件的默认事件处理

  • 相关阅读:
    LeetCode 485. Max Consecutive Ones
    LeetCode 367. Valid Perfect Square
    LeetCode 375. Guess Number Higher or Lower II
    LeetCode 374. Guess Number Higher or Lower
    LeetCode Word Pattern II
    LeetCode Arranging Coins
    LeetCode 422. Valid Word Square
    Session 共享
    java NIO
    非阻塞IO
  • 原文地址:https://www.cnblogs.com/java-cjt/p/4060852.html
Copyright © 2011-2022 走看看