java - 在 Windows 7 上控制 setLocation 偏移 4px

标签 java windows-7 position swt

我注意到函数 Control#setLocation(int, int) 有问题。我用它来同步两个 Shell 之间的移动,即如果我移动第一个,第二个也会移动相同数量的像素。

一切正常,除了一件小事:调用 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();
    }
}

如果我以这种方式定位主窗口,则输出为

Rectangle {124, 100, 276, 143} -> (400, 100)

我在手动测量后得到以下结果:

enter image description here

这让我得出结论,有一个我不知道的偏移量/边界/边距/任何涉及的东西。

期望的结果是调用 setLocation(400, 100) 导致 Shell 位于“(400, 100)”而不是“(396 , 96)”(这应该是一个合理的假设,对吧?)。

有没有人详细了解为什么会出现这种情况以及如何解决?


更新

如果我使用 SWT.NO_TRIM 作为 otherShell 的样式,它工作得很好。所以一定和窗饰(框)有关系。

最佳答案

好的,我找到问题了。阅读 Shell 的文档我发现了这个:

public Shell(Shell parent)

Constructs a new instance of this class given only its parent. It is created with style SWT.DIALOG_TRIM.

SWT.DIALOG_TRIM(即 SWT.CLOSE | SWT.TITLE | SWT.BORDER)在 Windows 上似乎在 外壳

这个小例子说明了问题:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell();

    shell.pack();
    shell.open();
    shell.setBounds(0, 0, 100, 100);

    Shell childOne = new Shell(shell);
    childOne.pack();
    childOne.open();
    childOne.setBounds(0, 100, 100, 100);

    Shell childTwo = new Shell(SWT.DIALOG_TRIM);
    childTwo.pack();
    childTwo.open();
    childTwo.setBounds(0, 200, 100, 100);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

看起来像这样:

enter image description here

这很明显 SWT.DIALOG_TRIM 负责...

在我的例子中,我通过明确指定样式解决了这个问题:

new Shell(shell, SWT.SHELL_TRIM);

tl;dr

如果你想定位你的 Shell 像素完美,不要使用 SWT.DIALOG_TRIM,而是 SWT.SHELL_TRIM 或者不要完全不要将您的 Shell 创建为另一个 Shell 的子级。

关于java - 在 Windows 7 上控制 setLocation 偏移 4px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24736624/

相关文章:

java - Play Framework 2.3.1 内部类中的 Promise 异常处理

java - 组合圆弧和矩形时出现小间隙

java - Collections.shuffle() 真的足够随机吗?实际例子似乎否定了这种说法

.net - 如何在崩溃时创建进程小型转储而不在 Windows 7 上显式运行 AdPlus?

macos - 从VMware Fusion到Mac上的Bootcamp

visual-studio-2010 - Opencv2.1不在Win7中启动

html - HTML/CSS 中的定位/解析问题,以及向页面添加图像

html - 如何将粘性定位元素移出父 div?

jquery - CSS 位置失败

java - 在AIDE中使用克隆的Git repo Andengine编译报错