作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试制作一个可以使用 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=2
和 yTrim=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/
我正在尝试通过在 View 加载后来回更改 alpha 级别来使其中包含图像的 uibutton 缓慢脉动... 目前我正在这样做,但它什么也没做...... -(void)loopdyloop {
我在使用 UITableView 时遇到了这个问题,tableView 的 cells 高度不同,自动布局约束设置不当。当您向下滚动到 tableView 的底部并加载更多数据(无论使用 reload
我是一名优秀的程序员,十分优秀!