我已经创建了向导,我希望它始终位于中心位置,任何人都可以帮助我吗?

时间:2012-01-24 07:21:04

标签: java eclipse-plugin jface

private Point getFirstMonitorSize() { // Here Point is org.eclipse.swt.graphics.Point
    Display display = Display.getDefault();
    if (display != null) {
        Monitor[] monitors = display.getMonitors();
        if (monitors.length == 1) {
            Rectangle clientArea = monitors[0].getClientArea();
            return new Point(clientArea.width / 2, clientArea.height / 2);
        }
    }
    return null;
}

我发现这个用于定位,但我不知道如何在向导对话框中使用?

1 个答案:

答案 0 :(得分:3)

对于Eclipse E4,您可以在E4LifeCycle类中添加此方法。 这将使您的应用程序中心成为主监视器。

这将使您的应用程序shell位置在屏幕的中心,如果您打开WizardDialog,您在其中设置了正确的parent shell,那么每件事都将落实到位

@Inject
@Optional
public void receiveActiveShell(@Named(IServiceConstants.ACTIVE_SHELL) final Shell shell) {
    try {
        if (shell != null) {
            final Display display = shell.getDisplay();
            final Monitor primary = display.getPrimaryMonitor();
            final Rectangle displayBounds = primary.getBounds();
            shell.setSize(displayBounds.width - 100, displayBounds.height - 100);
            final Point size = shell.getSize();
            Point defaultLocation = new Point((int) (displayBounds.width - size.x) / 2, (int) (displayBounds.height - size.y) / 2);
            shell.setLocation(this.defaultLocation);
        }
    } catch (Exception e) {
        LOGGER.error("Execption ocuured in Active Shell", e); //$NON-NLS-1$
    }
}