Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过在SWT中调用另一个类的方法来呈现该类的GUI?_Java_User Interface_Swt_Jface - Fatal编程技术网

Java 通过在SWT中调用另一个类的方法来呈现该类的GUI?

Java 通过在SWT中调用另一个类的方法来呈现该类的GUI?,java,user-interface,swt,jface,Java,User Interface,Swt,Jface,我有两个班,一个是助手。在助手类中,我有一些按钮和文本。我想使用这个类gui另一个类是Widget。为此,我在Helper中使用了静态方法。我从Widget类调用这个方法,但是我无法在我的Widget类中呈现Helper类gui。如果我使用了一些错误的术语,请帮助我。提前谢谢 这是我的全部代码 import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.Grid

我有两个班,一个是助手。在助手类中,我有一些按钮和文本。我想使用这个类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();
    }

}

您需要创建Helper类的对象,因为它扩展了composite,所以它的行为就像一个容器,您可以调整或设置Helper类的布局和布局数据