2014-05-18 92 views
1

我有两个班,一个是助手。在助手类中,我有一些按钮和文本。我想用这个类gui的另一个类是Widget。为此,我在Helper中有静态方法。我从Widget类调用这个方法,但是我不能在我的Widget类中呈现Helper类gui。如果我使用了一些错误的术语,请帮助我。提前致谢。从SWT调用其方法呈现另一个类的GUI?

这是我的完整代码。

import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Text; 

public class Helper extends Composite { 

    public Helper(Composite parent, int style) { 
     super(parent, style); 
     initGUI(this); 
    } 

    private void initGUI(Composite parent) { 
     parent.setLayout(new GridLayout(2, false)); 
     parent.setLayoutData(new GridData(GridData.FILL)); 

     Label label = new Label(parent, SWT.NONE); 
     label.setText("label:"); 
     label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 

     Text text = new Text(parent, SWT.SINGLE | SWT.BORDER); 
     text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 

     Button button = new Button(parent, SWT.PUSH); 
     button.setText("hi I am "); 
     button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 

     Button btn = new Button(parent, SWT.PUSH); 
     btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 
     btn.setText("hello"); 
    } 

    public static Composite show(Shell shell){ 
     shell = new Shell(); 
     Helper wid = new Helper(shell, SWT.NONE); 
     return wid; 
    } 

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

     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout()); 
     new Helper(shell, SWT.NONE); 
     shell.open(); 
     shell.setText("Helper"); 
     shell.pack(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 
} 


import org.eclipse.swt.SWT; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Text; 

public class Widget extends Composite { 

    public Widget(Composite parent, int style) { 
     super(parent, style); 
     initGUI(this); 
    } 

    private void initGUI(Composite parent) { 
     parent.setLayout(new GridLayout(3, false)); 
     parent.setLayoutData(new GridData()); 

     Text text = new Text(parent, SWT.SINGLE | SWT.BORDER); 
     text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 

     Button button = new Button(parent, SWT.PUSH); 
     button.setText("not Helper"); 
     button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1)); 

     Composite comp=Helper.show(parent.getShell()); 
     comp.setLayout(new GridLayout()); 
     comp.setLayoutData(new GridData()); 

    } 

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

     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout()); 
     new Widget(shell, SWT.NONE); 
     shell.open(); 
     shell.setText("New Widget"); 
     shell.pack(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 

} 

回答

2

你需要,因为它所以它像一个容器,并可以调整或设置布局和layoutdata到辅助类扩展复合创建Helper类的对象。

相关问题