无法启动向导页面

Unable to launch a Wizard Page

我对 UI 编码还很陌生,我必须创建多个向导页面,然后尝试从一个页面导航到另一个页面。我写了下面的代码,我在博客上读到了。但我无法启动它。

    public class MyWizardPage extends WizardPage {
        public MyWizardPage() {
            super("My Wizard");
        }

        public void createControl(Composite parent) {
            // Create the parent control
            Composite composite = new Composite(parent, SWT.NONE);
            composite.setLayout(new GridLayout(2, false));

            // Create some controls
            new Label(composite, SWT.LEFT).setText("Field #1:");
            Text field1 = new Text(composite, SWT.BORDER | SWT.SINGLE);
            field1.setLayoutData(new GridData(GridData.FILL_BOTH));

            new Label(composite, SWT.LEFT).setText("Field #2:");
            Text field2 = new Text(composite, SWT.BORDER | SWT.SINGLE);
            field2.setLayoutData(new GridData(GridData.FILL_BOTH));

            // Important!
            setControl(composite);
        }
    }

请在launching/running程序中帮助我。


修改:

public class MyWizardPage extends WizardPage {
        /******Newly added code****************/
        static Composite composite=null;

        public static void main(String[] args) {
            IWizard mypage= new MyWizardPage();
            WizardDialog dialog = new WizardDialog(composite, mypage);
            dialog.open();
        }

/******Newly added code****************/


        public MyWizardPage() {
            super("My Wizard");
        }

        public void createControl(Composite parent) {
            // Create the parent control
            composite = new Composite(parent, SWT.NONE);
            composite.setLayout(new GridLayout(2, false));

            // Create some controls
            new Label(composite, SWT.LEFT).setText("Field #1:");
            Text field1 = new Text(composite, SWT.BORDER | SWT.SINGLE);
            field1.setLayoutData(new GridData(GridData.FILL_BOTH));

            new Label(composite, SWT.LEFT).setText("Field #2:");
            Text field2 = new Text(composite, SWT.BORDER | SWT.SINGLE);
            field2.setLayoutData(new GridData(GridData.FILL_BOTH));

            // Important!
            setControl(composite);
        }


    }

我修改了上面的代码:

但是我收到以下错误: MyWizard Page 不是 IWizard 类型。我这样做是因为要实例化一个 WizardDialogue 对象,它需要 IWizard Type.I 试图为 MyWizardPageClass 实现 IWizard 接口,但它需要实现 18 个方法。我不确定如何进行。

请查看向导及其页面的以下代码: 请参阅打开向导的 TestWizard main 方法

第一页:

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class PageOne extends WizardPage {
    private Text text1;
    private Composite container;

    public PageOne() {
        super("First Page");
        setTitle("First Page");
        setDescription("Fake Wizard: First page");
    }

    @Override
    public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        Label label1 = new Label(container, SWT.NONE);
        label1.setText("Put a value here.");

        text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
        text1.setText("");
        text1.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!text1.getText().isEmpty()) {
                    setPageComplete(true);
                }
            }    
        });
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        text1.setLayoutData(gd);
        setControl(container);
        setPageComplete(false);
    }

    public String getText1() {
        return text1.getText();
    }
}

第二页:

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class PageTwo extends WizardPage {
    private Text text1;
    private Composite container;

    public PageTwo() {
        super("Second Page");
        setTitle("Second Page");
        setDescription("Fake Wizard: Second page");
    }

    @Override
    public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        Label label1 = new Label(container, SWT.NONE);
        label1.setText("Put a value here.");

        text1 = new Text(container, SWT.BORDER | SWT.SINGLE);
        text1.setText("");
        text1.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (!text1.getText().isEmpty()) {
                    setPageComplete(true);
                }
            }
        });
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        text1.setLayoutData(gd);
        setControl(container);
        setPageComplete(false);
    }

    public String getText1() {
        return text1.getText();
    }
}

演示向导:

import org.eclipse.jface.wizard.Wizard;

public class DemoWizard extends Wizard {
    protected PageOne one;
    protected PageTwo two;

    public DemoWizard() {
        super();
        setNeedsProgressMonitor(true);
    }

    @Override
    public String getWindowTitle() {
        return "Export My Data";
    }

    @Override
    public void addPages() {
        one = new PageOne();
        two = new PageTwo();
        addPage(one);
        addPage(two);
    }

    @Override
    public boolean performFinish() {
        // Print the result to the console
        System.out.println(one.getText1());
        System.out.println(two.getText1());

        return true;
    }
}

主要测试class :

import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestWizard {
    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3, true));

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Open Wizard");

        button.addListener(SWT.Selection, event -> {
            WizardDialog wizardDialog = new WizardDialog(shell, new DemoWizard());
                if (wizardDialog.open() == Window.OK) {
                    System.out.println("Ok pressed");
                } else {
                    System.out.println("Cancel pressed");
                }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}