无法启动向导页面

时间:2018-08-06 16:07:00

标签: eclipse-plugin swt

我对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);
        }
    }

请帮助我启动/运行程序。


修改:

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页面不是IWizard类型。我这样做是因为要实例化需要IWizard Type的WizardDialogue对象,我尝试为MyWizardPageClass实现IWizard interafce,但它需要实现18种方法。我不确定如何进行。

1 个答案:

答案 0 :(得分:1)

请通过以下向导代码及其页面: 请参阅打开向导的TestWizard主要方法

第一页:

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;
    }
}

主要测试班:

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();
    }
}