2011-05-14 65 views
1

在Java SWT shell窗口中,如何設置其內部大小而不是整個窗口框架大小?例如,如果我使用shell.setSize(300,250),這將使整個窗口顯示爲精確的300x250。這個300x250包括窗口框架的大小。設置Java SWT shell窗口內部區域的大小

如何設置內部大小,即將shell窗口的內容顯示區域設置爲300x250?這就是300x250不包括窗框的寬度。

我試圖減去一些偏移值,但事情是不同的操作系統有不同的窗口框架大小。所以有一個恆定的抵消將不準確。

謝謝。

回答

7

從你的問題我明白的是,你想設置Client Area的尺寸。而在SWT行話把它定義爲a rectangle which describes the area of the receiver which is capable of displaying data (that is, not covered by the "trimmings").

不能直接設置的Client Area尺寸,因爲沒有爲它的API。雖然你可以通過一點點破解來實現這一點。在下面的示例代碼中,我希望我的客戶區域爲300 by 250。爲了實現這一點,我使用了shell.addShellListener()事件監聽器。當外殼完全活動時(請參閱public void shellActivated(ShellEvent e)),然後我計算不同的邊距並再次設置我的外殼的大小。殼體尺寸的計算和重置給我所需的殼體尺寸。

>>Code:

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.ShellEvent; 
import org.eclipse.swt.events.ShellListener; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Menu; 
import org.eclipse.swt.widgets.Shell; 

public class MenuTest { 

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

     GridLayout layout = new GridLayout(); 
     layout.marginHeight = 0; 
     layout.marginWidth = 0; 
     layout.horizontalSpacing = 0; 
     layout.verticalSpacing = 0; 
     layout.numColumns = 1; 
     shell.setLayout(layout); 
     shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true)); 
     final Menu bar = new Menu (shell, SWT.BAR); 
     shell.setMenuBar (bar); 

     shell.addShellListener(new ShellListener() { 

      public void shellIconified(ShellEvent e) { 
      } 
      public void shellDeiconified(ShellEvent e) { 
      } 
      public void shellDeactivated(ShellEvent e) { 
      } 
      public void shellClosed(ShellEvent e) { 
       System.out.println("Client Area: " + shell.getClientArea()); 
      } 
      public void shellActivated(ShellEvent e) { 
       int frameX = shell.getSize().x - shell.getClientArea().width; 
       int frameY = shell.getSize().y - shell.getClientArea().height; 
       shell.setSize(300 + frameX, 250 + frameY); 
      } 
     });  

     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) display.sleep(); 
     } 
     display.dispose(); 
    } 
} 
+0

感謝您的幫助。你的例子非常有幫助!謝謝!! – Carven 2011-05-22 12:12:50

0

如果我找到你的權利,你應該設置內部組件的大小爲所需的大小,並使用方法pack()(框架)。

+0

我具有在所述框架的頂部的菜單控制。 pack()不考慮菜單欄的額外高度,而只是框架中的內容。如何獲得菜單欄的高度或讓pack()考慮菜單欄的大小? – Carven 2011-05-14 19:34:59

0
import org.eclipse.swt.SWT; 
import org.eclipse.swt.graphics.*; 
import org.eclipse.swt.widgets.*; 

public class SWTClientAreaTest 
{ 
    Display display; 
    Shell shell; 
    final int DESIRED_CLIENT_AREA_WIDTH = 300; 
    final int DESIRED_CLIENT_AREA_HEIGHT = 200; 

    void render() 
    { 
     display = Display.getDefault(); 
     shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); 

     Point shell_size = shell.getSize(); 
     Rectangle client_area = shell.getClientArea(); 

     shell.setSize 
     (
      DESIRED_CLIENT_AREA_WIDTH + shell_size.x - client_area.width, 
      DESIRED_CLIENT_AREA_HEIGHT + shell_size.y - client_area.height 
     ); 

     shell.open(); 

     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
      { 
       display.sleep(); 
      } 
     } 

     display.dispose(); 
    } 

    public static void main(String[] args) 
    { 
     SWTClientAreaTest appl = new SWTClientAreaTest(); 
     appl.render(); 
    } 
} 
+1

請添加一些描述答案的評論,並解答提出的具體問題。在StackOverflow中,純粹的代碼回答是皺眉 – clearlight 2016-12-31 06:45:16