How do I make it possible to resize a composite by dragging the corner

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 19:20:35

问题


I am working with SWT and I would like to be able to resize a composite by dragging the corner of it, the same way that you can resize a shell. I'm sure someone out there has implemented a good solution. Thanks.


回答1:


I think what you are looking for can be implemented with org.eclipse.swt.widgets.Tracker

here is sample working code:

public static void main(String[] args) {

    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.open();

    final Composite b = new Composite(shell, SWT.NONE);
    b.setBounds(20, 20, 80, 80);
    b.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

    b.addListener(SWT.MouseDown, new Listener() {

      public void handleEvent(Event e) {

        Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE);
        tracker.setStippled(true);
        Rectangle rect = b.getBounds();
        tracker.setRectangles(new Rectangle[] { rect });
        if (tracker.open()) {
          Rectangle after = tracker.getRectangles()[0];
          b.setBounds(after);
        }
        tracker.dispose();
      }
    });
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }  


来源:https://stackoverflow.com/questions/12500208/how-do-i-make-it-possible-to-resize-a-composite-by-dragging-the-corner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!