JFACE第四天 向导对话框
內容重點:
向導對話框
內容:
1. 向導對話框,每個頁教要繼顧自WizardPage类构造方法,并需实现
createControl抽象方法:
2.构造方法有两种:
A.
WizardPage(String pageName);
B.
WizardPage(String pageName,String title,ImageDescriptor titleImage)
3.在createControl方法中创建向导对话框的控件后,不要忘记调用setControl方法.
否则将不能显示控件,
4.当创建组装各个向导页面类时应该注意以下几个问题:
向导必须断承自Wizard类,必须实现performFinish方法,
该方法为单击"完成"按钮后调用的方法.
将各个向导页面加入到向导中使用的是addPage方法.
便一定要按顺序加入.
另外可以通过覆盖父类中的canFinish方法.设置"完成"按钮的可用状态.
向导页:
package com.swtjface.wizardpage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
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;
import org.eclipse.swt.widgets.Shell;
public class WQ1 extends WizardPage {
protected WQ1() {
super(bookWizard.q1, "title", ImageDescriptor.createFromFile(WQ1.class,
"icons\\customer.gif"));
this.setMessage("本書怎麽樣");
}
public void createControl(Composite parent) {
// TODO Auto-generated method stub
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);
wizardtest.putDS("wq1", "test1");
setControl(composite);
}
}
第二页:
package com.swtjface.wizardpage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
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;
import org.eclipse.swt.widgets.Shell;
public class WQ2 extends WizardPage {
protected WQ2() {
super(bookWizard.q2, "title", ImageDescriptor.createFromFile(WQ1.class,
"icons\\customer.gif"));
this.setMessage("怎麽樣");
}
public void createControl(Composite parent) {
// TODO Auto-generated method stub
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);
wizardtest.putDS("wq2", "test2");
setControl(composite);
}
}
第三页:
package com.swtjface.wizardpage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
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;
import org.eclipse.swt.widgets.Shell;
public class thanks extends WizardPage {
protected thanks() {
super(bookWizard.q3, "thanks", ImageDescriptor.createFromFile(
thanks.class, "icons\\customer.gif"));
this.setMessage("怎麽樣");
}
public void createControl(Composite parent) {
// TODO Auto-generated method stub
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
new Label(composite, SWT.LEFT).setText("thanks !!!");
wizardtest.putDS("wq3", "test3"); //记录信息到XML文档
setControl(composite);
}
}
装配程式:
package com.swtjface.wizardpage;
import org.eclipse.jface.wizard.Wizard;
public class bookWizard extends Wizard {
public static final String q1 = "WQ_1";
public static final String q2 = "WQ_2";
public static final String q3 = "thanks";
private WQ1 wq1;
private WQ2 wq2;
private thanks thanks;
public bookWizard() {
wq1 = new WQ1();
wq2 = new WQ2();
thanks = new thanks();
this.addPage(wq1);
this.addPage(wq2);
this.addPage(thanks);
this.setWindowTitle("Wizard page!!");
}
@Override
public boolean canFinish() {
if (this.getContainer().getCurrentPage() == thanks) {
return true;
} else {
return false;
}
}
@Override
public boolean performFinish() {
wizardtest.saveDS(); //保存
return false;
}
}
主程式:
package com.swtjface.wizardpage;
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;
import org.eclipse.swt.widgets.Shell;
public class wizardtest extends ApplicationWindow {
static DialogSettings ds = new DialogSettings(null);
public static void putDS(String key, String str) {
ds.put(key, str);
}
public static void saveDS() {
try {
ds.save("d:\\test.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public wizardtest() {
super(null);
// TODO Auto-generated constructor stub
}
protected Control createContents(Composite parent) {
parent.setLayout(new RowLayout(SWT.VERTICAL));
Button btn = new Button(parent, SWT.NONE);
btn.setText("打開向導");
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
WizardDialog dlg = new WizardDialog(Display.getCurrent()
.getActiveShell(), new bookWizard());
dlg.open();
}
});
return parent;
}
public static void main(String[] args) {
wizardtest test = new wizardtest();
test.setBlockOnOpen(true);
test.open();
Display.getCurrent().dispose();
}
}
如何实现各式各样的提示窗口呢,SWT提供了一个类
MessageDialog
只有一个确定
MessageDialog.openInformation(shell, "title", "message");
有是/否
MessageDialog.openConfirm(shell, "title","message");
有是/否/取消
MessageDialog dialog = new MessageDialog(shell, "title", null, "message",
MessageDialog.QUESTION, new String[] {IDialogConstants.YES_LABEL,
IDialogConstants.NO_LABEL,IDialogConstants.CANCEL_LABEL }, 0);
dialog.open();
还可以加入更多的选择项,只需要在数组中加入更多的内容
那如何取得点击了哪个按钮呢,两种方法
直接int result = dialog.open();
或者int result = dialog.getReturnCode();
返回的result的值就是被选中按钮在数组中的index