插件开发:视图插件中的按钮组合

时间:2017-03-08 12:44:11

标签: eclipse button plugins swt zest

我正在使用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);
}

1 个答案:

答案 0 :(得分:1)

如果您想在图表顶部显示按钮,则应使用FormLayout代替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);
}
相关问题