簡體   English   中英

渲染另一個類的GUI,使其無法在SWT中調用其方法?

[英]Rendering GUI of another class from calling its method in SWT?

我有兩個班級,一個是Helper。 在助手類中,我有一些按鈕和Text。 我想使用該類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類的對象,因為它擴展了復合材料,因此其行為類似於容器,並且可以調整或設置helper類的layout和layoutdata。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM