SWT 菜单导致布局裁剪

发布于 2024-12-09 11:55:16 字数 2054 浏览 0 评论 0原文

我是第一次尝试 SWT,我不知道如何使用菜单栏以及任何填充空间的布局。组合的布局是在不考虑菜单的情况下计算的,因此一旦添加菜单,布局的底部就会被裁剪。

令人惊讶的是,当用户调整大小时,菜单会被考虑在内,并且布局也很好。但我不知道如何以编程方式修复它; shell.layout(true,true)shell.setSize(250,250)shell.pack() 不能解决问题。

菜单导致布局剪辑。

package com.appspot.htmldoodads.pdfstuffs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuLayout {
    public static void main(String[] argv) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        GridLayout shellLayout = new GridLayout();
        shell.setLayout(shellLayout);

        // Add a menu bar with File -> Open...
        Menu bar = new Menu(shell, SWT.BAR);
        shell.setMenuBar(bar);
        MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
        fileItem.setText("&File");
        Menu subMenu = new Menu(shell, SWT.DROP_DOWN);
        fileItem.setMenu(subMenu);
        MenuItem openItem = new MenuItem(subMenu, SWT.CASCADE);
        openItem.setText("&Open...");

        // Add a button that fills the space.
        Button big = new Button(shell, SWT.PUSH);
        big.setText("Fill.");
        GridData bigLayoutData = new GridData(GridData.FILL, GridData.FILL, true, true);
        big.setLayoutData(bigLayoutData);

        // Add a button that doesn't fill space.
        new Button(shell, SWT.PUSH).setText("Regular");

        shell.layout(true, true);
        shell.setSize(250,250);
        shell.open();
        while ( ! shell.isDisposed ()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

I'm trying SWT for the first time, and I can't figure out how to use a Menu bar along with any space-filling layout. The Composite's layout is calculated without taking the Menu into account, so that once the Menu is added the bottom of the layout gets clipped.

Amazingly, when the user resizes, the Menu is taken into account and the layout is fine. But I can't figure out how to fix it programmatically; shell.layout(true,true), shell.setSize(250,250), shell.pack() don't fix the problem.

Menu causes layout to clip.After resize, everything is fine.

package com.appspot.htmldoodads.pdfstuffs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuLayout {
    public static void main(String[] argv) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        GridLayout shellLayout = new GridLayout();
        shell.setLayout(shellLayout);

        // Add a menu bar with File -> Open...
        Menu bar = new Menu(shell, SWT.BAR);
        shell.setMenuBar(bar);
        MenuItem fileItem = new MenuItem(bar, SWT.CASCADE);
        fileItem.setText("&File");
        Menu subMenu = new Menu(shell, SWT.DROP_DOWN);
        fileItem.setMenu(subMenu);
        MenuItem openItem = new MenuItem(subMenu, SWT.CASCADE);
        openItem.setText("&Open...");

        // Add a button that fills the space.
        Button big = new Button(shell, SWT.PUSH);
        big.setText("Fill.");
        GridData bigLayoutData = new GridData(GridData.FILL, GridData.FILL, true, true);
        big.setLayoutData(bigLayoutData);

        // Add a button that doesn't fill space.
        new Button(shell, SWT.PUSH).setText("Regular");

        shell.layout(true, true);
        shell.setSize(250,250);
        shell.open();
        while ( ! shell.isDisposed ()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-12-16 11:55:16

当我完成这个问题时,我想到了一个解决方案。我需要调用 shell.layout() after shell.open(),以便 GTK 可以在 SWT 计算可用空间之前渲染菜单用于布局。

I figured out a solution just as I was finishing the question. I needed to call shell.layout() after shell.open(), so that GTK can render the Menu before SWT calculates the available space for layout.

¢蛋碎的人ぎ生 2024-12-16 11:55:16

添加到 yonran 的答案:

如果使用 JFace 和 ApplicationWindow 作为您的主入口点,为您执行 shell.open() 。因此,您必须使用典型的 SWT 循环,而不是使用 JFace 阻塞 applicationWindow.open() 方法调用。

JFace 之前(不起作用,并且存在问题中描述的剪切问题):

public static void main(String[] args) {
    ApplicationWindow window = new MyApplicationWindow("My Window Title");
    window.setBlockOnOpen(true);
    window.open();
    Display.getCurrent().dispose()
}

JFace 之后(正确显示,没有任何剪切):

public static void main(String[] args) {
    ApplicationWindow window = new MyApplicationWindow("My Window Title");
    window.open();

    Shell shell = window.getShell();
    shell.layout(true, true);
    while (!shell.isDisposed())
        if (!Display.getCurrent().readAndDispatch())
            Display.getCurrent().sleep();
    Display.getCurrent().dispose();
}

Adding to yonran's answer:

If using JFace and and a subclass of ApplicationWindow as your main entry point, shell.open() is performed for you. So instead of using the JFace blocking applicationWindow.open() method call, you have to use the typical SWT loop.

JFace Before (Didn't work, and had clipping issue described in question):

public static void main(String[] args) {
    ApplicationWindow window = new MyApplicationWindow("My Window Title");
    window.setBlockOnOpen(true);
    window.open();
    Display.getCurrent().dispose()
}

JFace After (Correctly displays without any clipping):

public static void main(String[] args) {
    ApplicationWindow window = new MyApplicationWindow("My Window Title");
    window.open();

    Shell shell = window.getShell();
    shell.layout(true, true);
    while (!shell.isDisposed())
        if (!Display.getCurrent().readAndDispatch())
            Display.getCurrent().sleep();
    Display.getCurrent().dispose();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文