下一代的B/S开发框架--Echo 教程(4)

来源:互联网 发布:linux系统常用命令 编辑:程序博客网 时间:2024/04/27 21:27

模式窗口的实现

熟悉浏览器的人都知道, 浏览器上很难实现模式窗口, 就是那种一直在最上面的子窗口并且能禁止对父窗口的操作. 在B/S编程中碰到这种问题一般都会返回一个新页面, 然后用链接回到原来的页面. 有时候新窗口中的信息很少, 不得不想办法来装饰它. 我们看看用Echo怎么做:

import nextapp.echo.*;import nextapp.echo.event.*;/** * 模式窗口 */public class CommonWindow extends Window implements WindowListener {    protected Window parent; //父窗口    public CommonWindow(Window parent, boolean modal){        this.parent = parent;        if (modal && parent!=null)          parent.setVisible(false);    }    public CommonWindow(){}    protected void init(){        setDefaultCloseOperation(Window.DISPOSE_ON_CLOSE);        addWindowListener(this);    }    //窗口被dispose()时执行    public void windowClosed(WindowEvent e) {        if (parent!=null){            parent.setVisible(false); //激活父窗口            parent.setVisible(true);        }    }    public void windowClosing(WindowEvent e) {}    public void windowOpened(WindowEvent e) {}}

来看看上面的代码:

  1. 父窗口在构造函数中就被隐藏
  2. 子窗口监听自己被销毁的事件, 在被销毁后激活父窗口
  3. 如果窗口已经打开, 仅仅对窗口setVisable(true)并不能使其可见, 所以先隐藏再打开.

其它窗口一般扩展CommonWindow, 可以轻松实现小巧的alert/confirm窗口.

注意: EchoPoint里面提供了一种基于层的模式窗口:DialogPanel. IE对层的支持比较差, 仅供参考.

 

作为其它窗口的父类, 一般还要在里面设定窗口的缺省字体/颜色/布局, 以后只要调整CommonWindow就可以统一调整网站风格.

因为经常对窗口中所有按钮用同一个事件监听器, 所以CommonWindow中还增加这样一个投机取巧的方法:

    /**     * 对控件下的所有按钮增加相同的动作监听器, 忽略checkbox和radioBox     */    public static void addAllButtonListener(Component component,ActionListener listener){        Component[] all = component.getComponents();        for (int i = 0; i < all.length; i++) {            if (all[i] instanceof AbstractButton && !(all[i] instanceof ToggleButton))                ( (AbstractButton) all[i]).addActionListener(listener);            if (all[i] instanceof Component)                addAllButtonListener(all[i],listener);        }    }
原创粉丝点击