2011-10-13 86 views
1

我第一次尝试SWT,我无法弄清楚如何使用菜单栏以及任何空间填充布局。计算复合材料的布局时不考虑菜单,因此一旦添加了菜单,布局的底部就会被裁剪掉。SWT菜单导致布局剪辑

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

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

回答

3

我想通了,就像我在整理这个问题的解决方案。我需要在shell.open()之后调用shell.layout(),以便在SWT计算布局的可用空间之前,GTK可以呈现菜单。

0

添加到yonran的回答是:

如果使用的JFace和和你的主入口点的ApplicationWindow一个子类,shell.open()为您进行。因此,不要使用阻止applicationWindow.open()方法调用的JFace,您必须使用典型的SWT循环。

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