用 JFace 实现向导功能

本文实现了一个简单的向导(Wizard)。

BookSurveyWizard.java:

import org.eclipse.jface.wizard.Wizard;


public class BookSurveyWizard extends Wizard{
	public static final String Q1 = "QUESTION_1";
	public static final String Q2 = "QUESTION_2";
	public static final String THANKS = "THANKS";
	
	private QuestionOne one;
	private QuestionTwo two;
	private Thanks thanks;
	public BookSurveyWizard(){
		one = new QuestionOne();
		two = new QuestionTwo();
		thanks = new Thanks();
		
		this.addPage(one);
		this.addPage(two);
		this.addPage(thanks);
		this.setWindowTitle("读者调查向导");
	}
	
	public boolean canFinish(){
		if(this.getContainer().getCurrentPage() == thanks)
			return true;
		else
			return false;
	}
	
	public boolean performFinish(){
		return true;
	}
}

BookSurveyWizard 类是一个向导。该向导具有3个向导页,分别是 QuestionOne, QuestionTwo, Thanks。这些向导页继承了WizardPage类,并覆盖了createControl 方法。


QuestionOne.java:

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;


public class QuestionOne extends WizardPage{
	public QuestionOne(){
		super(BookSurveyWizard.Q1, "问题1:", ImageDescriptor.createFromFile(QuestionOne.class, "q.gif"));
		this.setMessage("您认为这本书的难度是:");
	}
	
	public void createControl(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		new Label(composite, SWT.LEFT).setText("A.");
		Button b1 =  new Button(composite, SWT.RADIO);
		b1.setText("太简单");
		b1.setSelection(true);
		new Label(composite, SWT.LEFT).setText("B.");
		Button b2 = new Button(composite, SWT.RADIO);
		b2.setText("还可以");
		new Label(composite, SWT.LEFT).setText("C.");
		Button b3 = new Button(composite, SWT.RADIO);
		b3.setText("太难");
		setControl(composite);
	}
}


 

QuestionTwo.java:

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;


public class QuestionTwo extends WizardPage{
	public QuestionTwo(){
		super(BookSurveyWizard.Q2, "问题2", ImageDescriptor.createFromFile(QuestionOne.class, "q.gif"));
		this.setMessage("您会考虑在今后的项目中使用SWT开发桌面程序吗:");
	}
	
	public void createControl(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		new Label(composite, SWT.LEFT).setText("A.");
		Button b1 = new Button(composite, SWT.RADIO);
		b1.setText("会");
		b1.setSelection(true);
		new Label(composite, SWT.LEFT).setText("B.");
		Button b2 = new Button(composite, SWT.RADIO);
		b2.setText("可能会");
		new Label(composite, SWT.LEFT).setText("C.");
		Button b3 = new Button(composite, SWT.RADIO);
		b3.setText("不会");
		setControl(composite);
	}
}

 

 

Thanks.java:

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;


public class Thanks extends WizardPage{
	protected Thanks(){
		super(BookSurveyWizard.THANKS, "感谢:",ImageDescriptor.createFromFile(QuestionOne.class, "q.gif"));
		this.setMessage("非常感谢您参加本次调查!");
	}
	
	public void createControl(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout());
		new Label(composite, SWT.CENTER).setText("感谢您的支持");
		setControl(composite);
	}
}


 

下面是一个测试类。

WizardTest.java:

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;


public class WizardTest extends ApplicationWindow{
	public WizardTest(){
		super(null);
	}
	
	protected Control createContents(Composite parent){
		parent.setLayout(new RowLayout(SWT.VERTICAL));
		Button button  = new Button(parent, SWT.NONE);
		button.setText("打开简单向导对话框");
		button.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e){
				//建立并打开打开向导对话框,该对话框使用了 BookSurveyWizard 向导
				WizardDialog dlg = new WizardDialog(Display.getCurrent().getActiveShell(),
						new BookSurveyWizard());
				dlg.open();
			}
		});
		return parent;
	}
	
	public static void main(String[] args){
		WizardTest test = new WizardTest();
		test.setBlockOnOpen(true);
		test.open();
		Display.getCurrent().dispose();
	}
}


从上可以看出,当点击按钮的时候,将会建立一个 WizardDialog,它实现了 IWizardContainer。它使用向导BookSurveyWizard。如下图:

 

另外,WizardPage 还具有 getNextPage 方法,用于设置下一页所显示的 WizardPage。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值