【问题标题】:SWT Menu causes the layout to clipSWT 菜单导致布局剪辑
【发布时间】:2011-10-13 08:58:14
【问题描述】:

我是第一次尝试 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();
    }
}

【问题讨论】:

    标签: swt


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      添加到 yonran 的答案:

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

      JFace Before(没有工作,并且有问题描述的剪裁问题):

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

      JFace After(正确显示,没有任何剪辑):

      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();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 2019-07-02
        相关资源
        最近更新 更多