我注意到功能Control#setLocation(int, int)
的一个问题。我用它来同步两个Shell
之间的移动,即如果我移动第一个,第二个移动相同数量的像素。控制集偏移4px在Windows 7上
这一切都正常工作,除了一件小事:调用setLocation()
后Shell
的实际位置偏移4像素(在Windows 7上,没有得到改变,在另一个操作系统上测试它)。
下面是说明该问题的示例程序:
public class StackOverflow
{
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell();
shell.setLayout(new GridLayout(5, true));
for (int i = 0; i < 25; i++)
new Label(shell, SWT.NONE).setText("Label: " + i);
final Shell otherShell = new Shell(shell);
otherShell.setLayout(new FillLayout());
new Button(otherShell, SWT.PUSH).setText("Button");
Listener synchronizeMovement = new Listener()
{
@Override
public void handleEvent(Event event)
{
Rectangle parentBounds = shell.getBounds();
Rectangle displayBounds = Display.getDefault().getBounds();
Point size = otherShell.getSize();
/* Check if there's sufficient space to the right of the parent */
if ((displayBounds.width - (parentBounds.width + parentBounds.x)) >= size.x)
{
otherShell.setLocation(parentBounds.x + parentBounds.width, parentBounds.y);
System.out.println(parentBounds + " -> (" + (parentBounds.x + parentBounds.width) + ", " + parentBounds.y + ")");
}
}
};
shell.addListener(SWT.Move, synchronizeMovement);
otherShell.addListener(SWT.Show, synchronizeMovement);
shell.pack();
shell.open();
otherShell.pack();
otherShell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}
如果我以这样的方式定位在主窗口,该输出是
矩形{124,100,276, 143} - >(400,100)
我得到以下人工测量后的结果:
这使我得出的结论是有一个抵消/边界/保证金/无论涉及,我不知道。
期望的结果是调用setLocation(400, 100)
导致Shell
位于“(400,100)”而不是“(396,96)”(这应该是一个合理的假设,对吗?)。
有没有人有详细的见解,为什么这是这种情况,以及如何解决它?
更新
如果我使用SWT.NO_TRIM
为otherShell
的风格它工作得很好。所以它必须与窗饰(框架)有关。
这是值得这项工作在Mac OS X上正确工作 –
谢谢@ greg-449,这是很好的知道。当我回家时,我会在Linux上尝试它。 – Baz
SWT是否有像AWT这样的getInsets()函数? getInsets()返回AWT中Frame的边框大小。也许SWT有类似的功能。 –