2011-10-13 2 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(); 
}