【问题标题】:Hide ON_TOP shell in SWT on Display minimimized在 SWT 中隐藏 ON_TOP shell on Display minimized
【发布时间】:2014-03-15 00:25:09
【问题描述】:

当显示最小化时,我试图隐藏一个 SWT 外壳。我遗漏了一些东西,非常感谢您的帮助。

附加信息:这个 shell 实际上是一个弹出窗口,当用户点击一个组合时会被绘制出来。最后,我的目标是在组合不可见时隐藏此弹出外壳(用户最小化窗口或在窗口之间切换,例如使用 Alt+Tab)。

这是我的代码:

static Shell middleClickNodeInfoShell ;
static Label nodeIdLabel ;

void init(){
    ... 
    /** Focused node on middle click*/
    middleClickNodeInfoShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.MODELESS);
    middleClickNodeInfoShell.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
    middleClickNodeInfoShell.setLayout(createNoMarginLayout(1, false));

    nodeIdLabel = new Label(middleClickNodeInfoShell, SWT.NONE);
    Display.getDefault().addListener(SWT.Iconify,new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            // TODO Auto-generated method stub
            middleClickNodeInfoShell.setVisible(false);
        }
    });
}   

@Override
public boolean onMouseClicked(Button button, ScreenPosition screenPos,
            final GeoPosition arg2) {
    ... 

    nodeIdLabel.setText("Node Id: "+node.getId());
    middleClickNodeInfoShell.setLocation(pos.getX()+displayX,pos.getY()+displayY+30);
    middleClickNodeInfoShell.setVisible(true);
    middleClickNodeInfoShell.pack();
}

【问题讨论】:

  • 我猜这不是完整的代码,发布完整的代码并添加更多细节来发布
  • 嘿Chandrayya,我添加了更多代码。干杯。
  • 此代码不完整,无法在其他用户机器上编译。添加 MORE 代码,以便我们直接复制您的代码并测试、验证并修复问题。
  • 此代码是带有专有包的 OSGI 环境的一部分。我去掉了专有部分。使用原始代码也无法编译。除非合适,否则请避免使用大写锁定。问题很简单:当鼠标在可见合成上时显示一个弹出窗口,当合成不再可见时隐藏它。
  • 我认为你不仅要处理 Iconify 事件,如果通过滚动看不到复合材料怎么办。当您向 Display 添加侦听器时,您的代码可能会导致内存泄漏,并且在释放复合材料时不要将其删除。

标签: java swt


【解决方案1】:

这里是示例代码,可帮助您确定要查找的内容

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setText("Shell Example");
    shell.setLayout(new RowLayout());

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Click Me");


    final Shell tip = new Shell(shell,SWT.MODELESS);
    tip.setLayout(new FillLayout());
    Label lbl = new Label(tip, SWT.NONE);
    lbl.setText("***tooltip***");
    tip.pack();
    shell.addControlListener(new ControlListener() {
      @Override
      public void controlResized(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
      @Override
      public void controlMoved(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
    });

    button.addSelectionListener(new SelectionAdapter() {

      public void widgetSelected(SelectionEvent event) {
        changeTipLocation(display, button, tip);
        tip.open();
      }

    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();

  }

  private static void changeTipLocation(final Display display, final Button button, final Shell tip) {

    Rectangle bounds = button.getBounds();
    Point loc = button.getLocation();
    tip.setLocation(display.map(button, null, new Point(loc.x+bounds.width, loc.y+bounds.height)));
  }

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 2019-11-06
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多