如题 请问SWT 有什么dialog的类函数能让dialogshell的出现位置始终出现在父窗口的中间呢?

解决方案 »

  1.   

    自己写一个静态类DisplayUtil好了。
    代码如下:
    /**
     * make the control display at the middle of the screen
     * 
     * @param control
     */
    public static void showOnAtScreenMiddle( Control control )
    { int screenWidth = Display.getDefault().getPrimaryMonitor().getBounds().width;
    int screenHeight = Display.getDefault().getPrimaryMonitor().getBounds().height; control.setLocation( ( screenWidth - control.getSize().x ) / 2, ( screenHeight - control.getSize().y ) / 2 ); } /**
     * Make the control disploy at the middle of the parent Composite
     * @author marquis
     * @Create Date: Jun 17, 2008
     * @param control
     */
    public static void setWidgetAtCenter( Control control )
    {
    if( control.getParent() == null )
    {//Top shell
    showOnAtScreenMiddle( control );
    return;
    } int parentWidth = control.getParent().getSize().x;
    int parentHeight = control.getParent().getSize().y; control.setLocation( ( parentWidth - control.getSize().x ) / 2, ( parentHeight - control.getSize().y ) / 2 );
    }
      

  2.   

     dlg.setLocationRelativeTo(null);
    的这个属性就OK了
      

  3.   


    这个是Swing的方法,SWT似乎没有这个方法吧。
    再有,这个是用于显示在整个屏幕的中央,而不是显示在父窗口的中间吧。
      

  4.   

    取得屏幕大小个窗体大小然后计算就可以实现了,代码如下public class Center { public static void centerShell(Shell shell)
    {
             //得到屏幕的宽度和高度
             int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
             int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
             //得到Shell窗口的宽度和高度
             int shellHeight = shell.getBounds().height;
             int shellWidth = shell.getBounds().width;
             //如果窗口大小超过屏幕大小,让窗口与屏幕等大
             if(shellHeight > screenHeight)
                       shellHeight = screenHeight;
             if(shellWidth > screenWidth)
                      shellWidth = screenWidth;
            //让窗口在屏幕中间显示
            shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) );
    }
    }