2010-12-08 86 views

回答

6

使用ControlListener捕獲窗口的resize事件。 如果用戶嘗試更改窗口寬度,請恢復其默認值。

import org.eclipse.swt.events.ControlAdapter; 
import org.eclipse.swt.events.ControlEvent; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class TestWindow { 

    private static int CONST_WIDTH = 200; 

    public static void main(String[] args) { 
     Display display = new Display(); 
     final Shell shell = new Shell(display); 
     shell.setBounds(shell.getBounds().x, shell.getBounds().y, CONST_WIDTH, shell.getBounds().height); 
     shell.setText("Test"); 

     shell.addControlListener(new ControlAdapter() { 

      @Override 
      public void controlResized(ControlEvent e) { 
       Rectangle rect = shell.getBounds(); 
       if(rect.width != CONST_WIDTH) { 
        shell.setBounds(rect.x, rect.y, CONST_WIDTH, rect.height); 
       } 
      } 
     }); 

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

它也會阻止移動窗口:`shell.setBounds(rect.x,rect.y,` – draw 2011-06-24 21:39:37