2017-03-08 21 views
0

我正在使用zest創建顯示圖形的插件。我正在嘗試創建刷新插件的按鈕。按鈕正在工作,我也在視圖中看到圖形。但是,按鈕在視圖中佔用了大量空間,並限制了圖形的位置。我希望按鈕能夠限制圖形的位置。我該如何解決它?由於![enter image description here]插件開發:視圖插件中的按鈕組合

public void createPartControl(Composite parent) { 

    //create refresh button 

    Composite container = new Composite(parent, SWT.NONE); 

    container.setLayout(new GridLayout(1, false)); 
    Button btnMybutton = new Button(container, SWT.PUSH); 
    btnMybutton.setBounds(0, 10, 75, 25); 
    btnMybutton.setText("Refresh Graph"); 
    btnMybutton.addSelectionListener(new SelectionListener() { 

     @Override 
     public void widgetSelected(SelectionEvent e) { 
     init(); 
     } 

     @Override 
     public void widgetDefaultSelected(SelectionEvent e) { 
     // TODO Auto-generated method stub 
     } 
    }); 

    // Graph will hold all other objects 
    graph = new Graph(parent, SWT.NONE); 
} 
+0

不要混用'setBounds' - 它不起作用。佈局將覆蓋邊界。您可以在視圖菜單上使用工具欄項目,該項目將顯示在最小化和最大化項目旁邊的右上角。 –

+0

謝謝!這是非常好的主意! – RoG

回答

1

如果你想顯示在圖上頂部的按鈕,你應該使用的FormLayout代替GridLayout:用`GridLayout`

public void createPartControl(Composite parent) { 

    //create refresh button 

    Composite container = new Composite(parent, SWT.NONE); 
    container.setLayout(new FormLayout()); // Use FormLayout instead of GridLayout 

    Button btnMybutton = new Button(container, SWT.PUSH); 
    // btnMybutton.setBounds(0, 10, 75, 25); This line is unnecessary 

    // Assign the right FormData to the button 
    FormData formData = new FormData(); 
    formData.left = new FormAttachment(0, 5); 
    formData.top = new FormAttachment(0, 5); 
    btnMybutton.setLayoutData(formData); 

    btnMybutton.setText("Refresh Graph"); 
    // Use SelectionAdapter instead of SelectionListener 
    // (it's not necessary but saves a few lines of code) 
    btnMybutton.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
     init(); 
     } 
    }); 

    // Graph will hold all other objects 
    graph = new Graph(container, SWT.NONE); // Note parent changed to container 

    // Assignt the right FormData to the graph 
    FormData formData2 = new FormData(); 
    formData2.left = new FormAttachment(0, 0); 
    formData2.top = new FormAttachment(0, 0); 
    formData2.right = new FormAttachment(100, 0); 
    formData2.bottom = new FormAttachment(100, 0); 
    graph.setLayoutData(formData2); 
} 
+0

謝謝!!!!!!!!! – RoG

+0

@RoG不客氣。 Upvotes也歡迎:) – ZhekaKozlov