gpt4 book ai didi

java - SWT.Tracker 鼠标从左到右,从上到下跳动

转载 作者:行者123 更新时间:2023-11-30 06:22:16 29 4
gpt4 key购买 nike

我尝试制作一个可以使用 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();
}
});

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

最佳答案

解决方法如下:当鼠标到达控件边缘时跟踪鼠标位置并显示正确的光标,然后相应地决定在 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();
}
};

关于java - SWT.Tracker 鼠标从左到右,从上到下跳动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19536427/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com