从SWT中调用其方法渲染另一个类的GUI?

时间:2014-05-18 11:20:28

标签: java user-interface swt jface

我有两个班,一个是帮手。在Helper类中,我有一些按钮和文本。我想使用这个类gui另一个类是Widget。为此我在Helper中有静态方法。我从Widget类调用这个方法,但是我不能在我的Widget类中渲染Helper类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();
    }

}

1 个答案:

答案 0 :(得分:2)

你需要创建Helper类的对象,因为它扩展了复合,因此它的行为就像一个容器,你可以调整或设置layout和layoutdata到helper类。