2014-12-08 47 views

回答

0

下面的代碼會在每次單擊相應按鈕時將窗扇向左或向右移動約1px。

private static int middle = 0; 

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

    final SashForm form = new SashForm(shell, SWT.HORIZONTAL); 

    final Button left = new Button(form, SWT.PUSH); 
    final Button right = new Button(form, SWT.PUSH); 

    left.setText("Left"); 
    right.setText("Right"); 

    Listener listener = new Listener() 
    { 
     @Override 
     public void handleEvent(Event event) 
     { 
      if(event.widget.equals(left)) 
       middle++; 
      else 
       middle--; 

      int[] weights = new int[]{1, 1}; 

      int width = form.getClientArea().width; 

      weights[0] = (int)Math.round(width/2f + middle); 
      weights[1] = (int)Math.round(width/2f - middle); 

      form.setWeights(weights); 
     } 
    }; 

    left.addListener(SWT.Selection, listener); 
    right.addListener(SWT.Selection, listener); 

    form.setWeights(new int[] { 1, 1 }); 

    shell.pack(); 
    shell.open(); 

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

是這樣的:左邊按鈕

enter image description here

點擊十次,它看起來像這樣:

enter image description here

+1

非常感謝您的回覆..上面的代碼在窗框區域創建按鈕,但是我需要在分隔線上顯示按鈕(成爲分隔線的一部分)。 – Pooja 2014-12-10 07:58:21

+0

@Pooja幾乎可以肯定這是不可能的... – Baz 2014-12-10 08:25:47

+0

@Pooja嗯,至少不是用'SashForm'。你是否需要通過拖動來調整它的大小,或者你只是想用按鈕來改變它? – Baz 2014-12-10 09:24:47