SWT.Tracker鼠标从左到右,从上到下跳跃

时间:2013-10-23 08:20:44

标签: java swt

我尝试制作一个复合材料,我可以使用SWT.Tracker调整大小,这里是我的示例代码

        final Composite b = new Composite(parent, SWT.NONE);
        b.setBounds(20, 20, 80, 80);
        b.setBackground(parent.getDisplay().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();
          }
        });

当我开始向左拖动时,光标会跳到合成的右边缘。当我开始向上拖动到底部时,光标跳到复合材料的底部边缘。我查看了该类的文档,但找不到任何设置来解决此问题。有人有指针吗?

3 个答案:

答案 0 :(得分:3)

以下是解决方案: 在到达控件边缘时跟踪鼠标位置并显示正确的光标,然后相应地决定在Tracker构造函数中设置哪种样式。 保持任何边缘或角落的修剪区域,以便用户可以轻松输入Tracker

例如,在X轴和Y轴上,您可以分别调用修剪xTrim=2yTrim=2,以便在X轴上它将是2像素宽,在Y轴上将是3像素宽的行进入Tracker;

声明一个监听器,更改光标并相应地调用Tracker

Listener    resizeAndMoveListener   = new Listener()
                                        {

                                            Point   point   = null;

                                            public void handleEvent(Event event)
                                            {
                                                mousePoint = MouseInfo.getPointerInfo().getLocation();
                                                sRect = shell.getBounds();
                                                compoRect = composite.getBounds();
                                                sRect = shell.getBounds();
                                                compoRect = composite.getBounds();
                                                topLeftX = sRect.x + compoRect.x + 8;
                                                topLeftY = sRect.y + compoRect.y + 31;

                                                topRightX = sRect.x + compoRect.x + compoRect.width + 7;
                                                topRightY = sRect.y + compoRect.y + 31;

                                                bottomLeftX = sRect.x + compoRect.x + 8;
                                                bottomLeftY = sRect.y + compoRect.y + 31 + compoRect.height;

                                                bottomRightX = sRect.x + compoRect.x + compoRect.width + 8;
                                                bottomRightY = sRect.y + compoRect.y + 31 + compoRect.height;

                                                xTrim = 2;
                                                yTrim = 3;
                                                switch (event.type)
                                                {
                                                case SWT.MouseDown:

                                                    // Top Left corner point    
                                                    if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
                                                            && mousePoint.y >= topLeftY && mousePoint.y <= topLeftY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZENE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP | SWT.LEFT;
                                                    }
                                                    // Top Edge
                                                    else if (mousePoint.x > topLeftX + xTrim && mousePoint.x < topRightX - xTrim && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEN);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP;
                                                    }
                                                    // Top Right corner Point

                                                    else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX && mousePoint.y >= topRightY && mousePoint.y <= topRightY + xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.TOP | SWT.RIGHT;
                                                    }
                                                    // Right edge 
                                                    else if (mousePoint.x >= topRightX - xTrim && mousePoint.x <= topRightX
                                                            && mousePoint.y > topRightY + xTrim && mousePoint.y < bottomRightY - xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.RIGHT;
                                                    }
                                                    // Bottom Left corner
                                                    else if (mousePoint.x >= bottomLeftX && mousePoint.x <= bottomLeftX + xTrim
                                                            && mousePoint.y >= bottomLeftY - xTrim && mousePoint.y <= bottomLeftY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM | SWT.LEFT;
                                                    }
                                                    // Left Edge 
                                                    else if (mousePoint.x >= topLeftX && mousePoint.x <= topLeftX + xTrim
                                                            && mousePoint.y > topLeftY + xTrim && mousePoint.y < bottomLeftY - xTrim)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.LEFT;
                                                    }
                                                    // Bottom Right corner
                                                    else if (mousePoint.x >= bottomRightX - xTrim && mousePoint.x <= bottomRightX
                                                            && mousePoint.y >= bottomRightY - xTrim && mousePoint.y <= bottomRightY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZESE);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM | SWT.RIGHT;
                                                    }

                                                    // Bottom Edge 
                                                    else if (mousePoint.x > bottomLeftX + xTrim && mousePoint.x < bottomRightX - xTrim
                                                            && mousePoint.y > bottomRightY - yTrim && mousePoint.y < bottomRightY)
                                                    {
                                                        shell.setCursor(CURSOR_SIZEN);
                                                        resizeAction = 1;
                                                        resizeStyle = SWT.BOTTOM;
                                                    }
                                                    else
                                                    {
                                                        shell.setCursor(CURSOR_ARROW);
                                                        resizeAction = 0;
                                                    }
                                                    if (resizeAction == 1)
                                                        resize(event, resizeStyle);

                                                    else if (event.button == 1)
                                                    {
                                                        // prepare for moving when mouse button is down and resizeAction ==0
                                                        point = new Point(event.x, event.y);

                                                    }

                                                    break;

                                                case SWT.MouseMove:

                                                    if (point == null)
                                                        break;

                                                    int x = point.x - event.x;

                                                    int y = point.y - event.y;

                                                    Control control = (Control) event.widget;

                                                    move(shell, control, x, y);

                                                    point = null;

                                                    break;

                                                }

                                            }

                                        };

定义调整大小,即Tracker

void resize(Event event, final int STYLE)


{

    Control control = (Control) event.widget;
    tracker = new Tracker(control.getParent(), SWT.RESIZE | STYLE);
    tracker.setStippled(true);
    Rectangle rect = control.getBounds();
    tracker.setRectangles(new Rectangle[] { rect });
    if (tracker.open())
    {
        Rectangle after = tracker.getRectangles()[0];
        control.setBounds(after);
    }
    tracker.dispose();
    resizeAction = 0;
}

不要忘记在鼠标上发布Tracker

Listener    releaseTrackerListener  = new Listener()
                                        {

                                            @Override
                                            public void handleEvent(Event even)
                                            {

                                                resizeAction = 0;
                                                tracker.dispose();
                                            }
                                        };

答案 1 :(得分:1)

如果您希望始终向右下方延伸,请将创建跟踪器的行更改为:

Tracker tracker = new Tracker(b.getParent(), SWT.RESIZE | SWT.RIGHT | SWT.DOWN);

您可以使用SWT.UP,SWT.DOWN,SWT.LEFT和SWT.RIGHT。

答案 2 :(得分:1)

AppWorks的解决方案非常出色。在shell上添加一个侦听器以设置从控件边界退出的光标,以便光标返回其原始箭头时,这也是一个好主意。