【问题标题】:shell.setLocation(x, y) with respect to parent and not client areashell.setLocation(x, y) 相对于父区域而不是客户区域
【发布时间】:2013-11-05 16:40:19
【问题描述】:

我有一个从父 shell 创建的自定义 swt shell。 我需要设置外壳相对于其父复合材料的位置。但是,因为我在 shell 上调用 setLocation(x, y),所以 setLocation(x,y) 现在相对于 clientArea 起作用。 有没有办法让 shell.setLocation(x,y) 相对于 PARENT 复合 NOT clientArea 工作? .即,即使父复合体在屏幕上调整大小/移动,自定义 Shell 也应始终保留在其父复合体中。

示例代码sn-p:

 class CustOmShellTest {
    customShell = new Shell(parent.getShell(), SWT.TOOL | SWT.CLOSE);
        customShell.setLayout(new GridLayout());
        customShell.setBackgroundMode(SWT.INHERIT_FORCE);
        customShell.setSize(300, 400);
        customShell.setLocation(parent.getBounds().x, parent.getBounds().y );

}

new CustOmShellTest(parentOfThisInstanceComposite);

//这是相对于 disPlay 定位的实例。我希望它相对于//与 parentOfThisInstanceComposite 进行定位

感谢任何帮助! 谢谢。

【问题讨论】:

    标签: java swt


    【解决方案1】:

    我创建了一个 sn-p,您的自定义外壳被锚定到主外壳中的一个组件。我称该组件为“锚”。将其替换为您的控件。

    这里的神奇方法是Control.toDisplay(),为了保持位置你必须添加resize和move listeners。

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
    
        final Label label = new Label(shell, SWT.NONE);
        label.setText("anchor");
        label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
    
        final Shell customShell = new Shell(shell, SWT.TOOL | SWT.CLOSE);
        customShell.setLayout(new GridLayout());
        customShell.setBackgroundMode(SWT.INHERIT_FORCE);
        customShell.setSize(300, 400);
        customShell.setVisible(true);
    
        final Listener listener = new Listener() {
            @Override
            public void handleEvent(Event event) {
                final Rectangle bounds = label.getBounds();
                final Point absoluteLocation = label.toDisplay(bounds.width, bounds.height);
                customShell.setLocation(absoluteLocation);
                if (shell.getMaximized()) {
                    display.asyncExec(new Runnable() {
                        @Override
                        public void run() {
                            handleEvent(null);
                        }
                    });
                }
            }
        };
        shell.addListener(SWT.Resize, listener);
        shell.addListener(SWT.Deiconify, listener);
        shell.addListener(SWT.Move, listener);
        customShell.addListener(SWT.Move, listener);
    
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    

    请注意,我还在自定义外壳上添加了侦听器,以确保它永远不会移动。当父 shell 移动时,自定义 shell 也随之移动。

    【讨论】:

      【解决方案2】:

      下面的代码让我得到实际的相对位置 自定义外壳的父级。这需要通过 RESIZE 事件来完成,即

      //parent.toDisplay(parent.getLocation().x , // parent.getLocation().y)

      customShell.addListener(SWT.RESIZE, new Listener() {
                  public void handleEvent(final Event event) {
      
      customShell.setLocation(parent.toDisplay(parent.getLocation().x ,
                              parent.getLocation().y));                
              customShell.layout();
                      parent.layout();
                  }
              });
      

      感谢您的意见

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-04
        相关资源
        最近更新 更多