关于JFace的自定义对话框(Dialog类)

仅仅是使用MessageDialog,InputDialog等JFace中现成的对话框类是无法满足实际项目开发需要的.

很多时候都需要自己定制对话框,自定义对话框只要在Dialog类的基础上作扩展就行了.

1.起步:扩展Dialog类

 1 //注意:JFace包和SWT包都有一个Dialog类,我们继承的是JFace的
 2 public class MyDialog extends Dialog {
 3     public MyDialog(Shell parentShell) {
 4         super(parentShell);
 5     }
 6     // 在这个方法里构建Dialog中的界面内容
 7     @Override
 8     protected Control createDialogArea(Composite parent) {
 9         // 建议不要直接在parent上创建组件,否则容易导致界面布局混乱。 应该象本例一样再叠加一个topComp面板,然后在此面板创建及布局
10         return null;
11 
12     }
13 }

 

"@Override"是JDK5.0的注解标签,表示该方法必须是改写自父类的方法,该注解可要可不要,不影响程序的运行.以上代码建立了一个叫做MuyDialog的自定义对话框,,他的使用方法和JFace自带的InputDialog一样.如下:

        // ---------创建窗口中的其他界面组件-------------
        MyDialog dialog = new MyDialog(shell);
        dialog.open();

 

运行结果:

关于JFace的自定义对话框(Dialog类)

2.创建界面组件.

现在通过在createDialogArea方法中写入代码,来给MyDialog加入界面组件,这些代码和以前的界面方法是一样的,只不过以前是用Shell作为容器创建的组件,现在则是在createDialogArea方法参数提供的"Composite parent"容器上创建组件.

对于createDialogArea()方法修改如下:

 1     // 在这个方法里构建Dialog中的界面内容
 2     @Override
 3     protected Control createDialogArea(Composite parent) {
 4         // 建议不要直接在parent上创建组件,否则容易导致界面布局混乱。 应该象本例一样再叠加一个topComp面板,然后在此面板创建及布局
 5         Composite topComp = new Composite(parent, SWT.NONE);
 6         topComp.setLayout(new RowLayout());// 应用RowLayout面局
 7         new Label(topComp, SWT.NONE).setText("请输入:");// 加入一个文本标签
 8         Text text = new Text(topComp, SWT.BORDER);// 加入一个文本框
 9         text.setLayoutData(new RowData(100, -1));// 用RowData来设置文本框的长度
10         return topComp;
11     }

 

createDialogArea方法的返回值类型要求是Control,其谱系图如下所示:

关于JFace的自定义对话框(Dialog类)

可见Composite是Control的子类,所以上面的程序中返回topComp对象也是可以的.

运行结果:

关于JFace的自定义对话框(Dialog类)

3.

改变对话框的式样:

上面的运行结果在对话框右上角只有一个关闭按钮,如果想让器其有最小化,最大化按钮,并且窗口可用鼠标改变大小,则只需要再MyDialog类中改写父类的getShellStyle方法即可,如果让此方法只返回SWT.NONE式样,则得到如下的结果.代码如下:

如果是这个设置return super.getShellStyle();  或者是 return super.getShellStyle() |SWT.NONE;
对应的输入框如下:

关于JFace的自定义对话框(Dialog类)

 

1     // 改写父类Dialog的getShellStyle方法可以改变窗口的默认式样
2     @Override
3     protected int getShellStyle() {
4         // 用super.getShellStyle()得到原有式样,再附加上两个新式样
5         return super.getShellStyle() | SWT.RESIZE | SWT.MAX;
6         //return super.getShellStyle();
7     }

 

 如果是这个设置return super.getShellStyle() | SWT.RESIZE | SWT.MAX;
对应的输入框如下:

关于JFace的自定义对话框(Dialog类)

4.定制对话框的按钮:

如果想在对话框中增加按钮,则需要改写父类的initializeBounds方法,如果还想改变"确定,取消"两个按钮的名称,则需要再改写父类的createButton方法,让它空实现,

如下图:

 

关于JFace的自定义对话框(Dialog类)

 1     // 改写父类创建按钮的方法使其失效。参数parent:取得放置按钮的面板;参数id:定义按钮的ID值;
 2     // 参数label:按钮文字;参数defaultButton:是否为Dialog的默认按钮。
 3     protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
 4         return null;
 5     }
 6 
 7     // 改写父类的initializeBounds方法,并调用父类的createButton方法建立按钮
 8     public static final int APPLY_ID = 101; // 定义好“应用”按钮的ID值(整型常量)
 9 
10     protected void initializeBounds() {
11         Composite comp = (Composite) getButtonBar();// 取得按钮面板
12         super.createButton(comp, APPLY_ID, "应用", true);
13         super.createButton(comp, IDialogConstants.OK_ID, "真的", false);
14         super.createButton(comp, IDialogConstants.CANCEL_ID, "算了", false);
15         super.initializeBounds();
16     }

 

5.改变自定义对话框的大小

在类中改写父类的getInitialSize即可,代码如下:

    // 改变对话框大小为宽300,高400(单位:像素)
    protected Point getInitialSize() {
        return new Point(300, 400);// super.getInitialSize()可以得到原来对话框的大小
    }

 

如上的两个MyDialog.java和MyDialogClient.java

MyDialog.java

 1 package cn.com.chengang.jface.dialog;
 2 
 3 import org.eclipse.jface.dialogs.Dialog;
 4 import org.eclipse.jface.dialogs.IDialogConstants;
 5 import org.eclipse.swt.SWT;
 6 import org.eclipse.swt.graphics.Point;
 7 import org.eclipse.swt.layout.RowData;
 8 import org.eclipse.swt.layout.RowLayout;
 9 import org.eclipse.swt.widgets.Button;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.swt.widgets.Text;
15 
16 //注意:JFace包和SWT包都有一个Dialog类,我们继承的是JFace的
17 public class MyDialog extends Dialog {
18     public MyDialog(Shell parentShell) {
19         super(parentShell);
20     }
21 
22     // 在这个方法里构建Dialog中的界面内容
23     @Override
24     protected Control createDialogArea(Composite parent) {
25         // 建议不要直接在parent上创建组件,否则容易导致界面布局混乱。 应该象本例一样再叠加一个topComp面板,然后在此面板创建及布局
26         Composite topComp = new Composite(parent, SWT.NONE);
27         topComp.setLayout(new RowLayout());// 应用RowLayout面局
28         new Label(topComp, SWT.NONE).setText("请输入:");// 加入一个文本标签
29         Text text = new Text(topComp, SWT.BORDER);// 加入一个文本框
30         text.setLayoutData(new RowData(100, -1));// 用RowData来设置文本框的长度
31         return topComp;
32     }
33 
34     // 改写父类Dialog的getShellStyle方法可以改变窗口的默认式样
35     @Override
36     protected int getShellStyle() {
37         // 用super.getShellStyle()得到原有式样,再附加上两个新式样
38         return super.getShellStyle() | SWT.RESIZE | SWT.MAX;
39     }
40 
41     // 改写父类创建按钮的方法使其失效。参数parent:取得放置按钮的面板;参数id:定义按钮的ID值;
42     // 参数label:按钮文字;参数defaultButton:是否为Dialog的默认按钮。
43     protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
44         return null;
45     }
46 
47     // 改写父类的initializeBounds方法,并调用父类的createButton方法建立按钮
48     public static final int APPLY_ID = 101; // 定义好“应用”按钮的ID值(整型常量)
49 
50     protected void initializeBounds() {
51         Composite comp = (Composite) getButtonBar();// 取得按钮面板
52         super.createButton(comp, APPLY_ID, "应用", true);
53         super.createButton(comp, IDialogConstants.OK_ID, "真的", false);
54         super.createButton(comp, IDialogConstants.CANCEL_ID, "算了", false);
55         super.initializeBounds();
56     }
57 
58     // 改变对话框大小为宽300,高400(单位:像素)
59     protected Point getInitialSize() {
60         return new Point(300, 400);// super.getInitialSize()可以得到原来对话框的大小
61     }
62 
63 }

MyDialogClient.java

 1 package cn.com.chengang.jface.dialog;
 2 
 3 import org.eclipse.swt.widgets.Display;
 4 import org.eclipse.swt.widgets.Shell;
 5 
 6 //注意:JFace包和SWT包都有一个Dialog类,我们继承的是JFace的
 7 public class MyDialogClient {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------创建窗口中的其他界面组件-------------
13         MyDialog dialog = new MyDialog(shell);
14         dialog.open();
15         // -----------------END------------------------
16         shell.dispose();
17         display.dispose();
18     }
19 }

 

 运行结果:

关于JFace的自定义对话框(Dialog类)

 

对话框的设置与取值:

在实际开发中,经常需要设置对话框中的组件的初始值,并在单击对话框的"确定"按钮后,取出对话框中的值进行相应的处理.实现对话框设置取值的关键是为各组件创建对应的变量来保存值.如下MyDialog2类所示:

MyDialog2.java

 1 package cn.com.chengang.jface.dialog;
 2 
 3 import org.eclipse.jface.dialogs.Dialog;
 4 import org.eclipse.jface.dialogs.IDialogConstants;
 5 import org.eclipse.swt.SWT;
 6 import org.eclipse.swt.layout.RowData;
 7 import org.eclipse.swt.layout.RowLayout;
 8 import org.eclipse.swt.widgets.Composite;
 9 import org.eclipse.swt.widgets.Control;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Shell;
12 import org.eclipse.swt.widgets.Text;
13 
14 public class MyDialog2 extends Dialog {
15     private String textValue; // 用来保存Text值的变量
16     private Text text;// 将文本写为类实例变量,否则其他方法无法访问它
17 
18     public MyDialog2(Shell parentShell) {
19         super(parentShell);
20     }
21 
22     public String getTextValue() {
23         return this.textValue;
24     }
25 
26     public void setTextValue(String value) {
27         this.textValue = value;
28     }
29 
30     // 在这个方法里构建Dialog中的界面内容
31     protected Control createDialogArea(Composite parent) {
32         Composite topComp = new Composite(parent, SWT.NONE);
33         topComp.setLayout(new RowLayout());
34         new Label(topComp, SWT.NONE).setText("请输入:");
35         text = new Text(topComp, SWT.BORDER);
36         // 把textValue设给Text作为初值,这时要注意对textValue作空值判断,给文本框设置空值是会出错的
37         text.setText(textValue == null ? "" : textValue);
38         text.setLayoutData(new RowData(100, -1));
39         return topComp;
40     }
41 
42     // 单击对话框底部按钮会执行此方法,参数buttonId是被单击按钮的ID值。
43     protected void buttonPressed(int buttonId) {
44         if (buttonId == IDialogConstants.OK_ID)// 如果单击确定按钮,则将值保存到变量
45             textValue = text.getText();
46         super.buttonPressed(buttonId);
47     }
48 }

 

 MyDialog2Client.java

 1 package cn.com.chengang.jface.dialog;
 2 
 3 import org.eclipse.jface.dialogs.IDialogConstants;
 4 import org.eclipse.swt.widgets.Display;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public class MyDialog2Client {
 8     public static void main(String[] args) {
 9         final Display display = Display.getDefault();
10         final Shell shell = new Shell();
11         shell.setSize(327, 253);
12         // ---------创建窗口中的其他界面组件-------------
13         MyDialog2 dialog = new MyDialog2(shell);
14         dialog.setTextValue("你好,中国"); // 设值
15         if (dialog.open() == IDialogConstants.OK_ID) {// 是否单击“确定”按钮
16             String str = dialog.getTextValue(); // 取值
17             System.out.println(str);
18         }
19         // -----------------END------------------------
20         shell.dispose();
21         display.dispose();
22     }
23 }

 

运行结果: 

关于JFace的自定义对话框(Dialog类)

 点击OK按钮:

关于JFace的自定义对话框(Dialog类)

也许有读者会问:为什么要在MyDialog2.java类中多加上一个textValue变量呢?难道直接对文本框对象text进行操作不行吗?例如text对象改成public前缀,然后如下使用:

MyDialog2.java中 先把text对象变成public类型的.

public Text text;

 

MyDialog2Client.java
// ---------创建窗口中的其他界面组件------------- MyDialog2 dialog = new MyDialog2(shell); dialog.setTextValue("你好,中国"); // 设值 if (dialog.open() == IDialogConstants.OK_ID) {// 是否单击“确定”按钮 String str = dialog.text.getText();//取值 System.out.println(str); }

 

上面的代码好像简洁了不少,可惜这样是行不通的.

弹出对话框之后点击OK,在控制台抛出的错误:

 关于JFace的自定义对话框(Dialog类)

因为在执行dialog. open()方法之前,对话框界面上的组件还没有创建,text文本框对象是空值,因此,dialog.text.setText()这一句会报出NullPointerException异常.

当单击"确定"按钮对话框退出,这时对话框中的组件将被销毁,所以对text做取值操作 也会包NullPointerException异常.正是由于这些原因,才不得不建立一个String类型的实例变量来保存文本框值做中转.

 

封装对话框中的数据到一个数据类:

往往对话框中会有很多的组件,如果没一个组件都需要一个变量来保存它的值,这样就会有很多变量.这样,对话框类就需要提供一个变量一对Setter/Getter方法.类的方法数量会暴涨,从而对Dialog类尝试了严重的污染.流行的MVC模式就是 要把界面和数据尽可能的剥离开来,因此应该把对话框的数据封装到一个数据类中.

关于JFace的自定义对话框(Dialog类)

Human.java

 1 public class Human {
 2     private String name; // 姓名
 3     private int old;// 年龄
 4     private boolean sex;// 性别
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13 
14     public int getOld() {
15         return old;
16     }
17 
18     public void setOld(int old) {
19         this.old = old;
20     }
21 
22     public boolean isSex() {
23         return sex;
24     }
25 
26     public void setSex(boolean sex) {
27         this.sex = sex;
28     }
29 
30 }

 

接下来参照TableViewer的setInput和getInput为自定义对话框类提供两个方法给Human数据类输入输出,并写好取出数据类给界面做初始化,及将界面值更新到数据类的代码,其代码如下所示:'

MyDialog3.java

 1 public class MyDialog3 extends Dialog {
 2     private Human human;
 3     private Text nameText;
 4     private Text oldText;
 5     private Button ggButton, mmButton;
 6 
 7     public MyDialog3(Shell parentShell) {
 8         super(parentShell);
 9     }
10 
11     public Human getInput() {
12         return this.human;
13     }
14 
15     public void setInput(Human human) {
16         this.human = human;
17     }
18 
19     // 在这个方法里构建Dialog中的界面内容
20     protected Control createDialogArea(Composite parent) {
21         Composite topComp = new Composite(parent, SWT.NONE);
22         topComp.setLayout(new GridLayout(2, false));
23         new Label(topComp, SWT.NONE).setText("姓名:");
24         nameText = new Text(topComp, SWT.BORDER);
25         nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
26         new Label(topComp, SWT.NONE).setText("年龄:");
27         oldText = new Text(topComp, SWT.BORDER);//省略了只能输入数值的限制
28         oldText.setLayoutData(new GridData(20,-1));
29         
30         new Label(topComp, SWT.NONE).setText("性别:");
31         Composite c = new Composite(topComp, SWT.None);
32         c.setLayout(new RowLayout());
33         ggButton = new Button(c, SWT.RADIO);
34         ggButton.setText("男");
35         mmButton = new Button(c, SWT.RADIO);
36         mmButton.setText("女");
37 
38         if (human != null) {
39             nameText.setText(human.getName() == null ? "" : human.getName());
40             oldText.setText(String.valueOf(human.getOld()));
41             ggButton.setSelection(human.isSex());
42             mmButton.setSelection(!human.isSex());
43         }
44         return topComp;
45     }
46 
47     protected void buttonPressed(int buttonId) {
48         if (buttonId == IDialogConstants.OK_ID) {// 如果单击确定按钮,则将值保存到Human对象中
49             if (human == null)
50                 human = new Human();
51             human.setName(nameText.getText());
52             human.setOld(Integer.parseInt(oldText.getText()));
53             human.setSex(ggButton.getSelection());
54         }
55         super.buttonPressed(buttonId);
56     }
57 }

 

使用这种自定义对话框类的客户端的代码如下:

MyDialog3Client.java

 1 public class MyDialog3 extends Dialog {
 2     private Human human;
 3     private Text nameText;
 4     private Text oldText;
 5     private Button ggButton, mmButton;
 6 
 7     public MyDialog3(Shell parentShell) {
 8         super(parentShell);
 9     }
10 
11     public Human getInput() {
12         return this.human;
13     }
14 
15     public void setInput(Human human) {
16         this.human = human;
17     }
18 
19     // 在这个方法里构建Dialog中的界面内容
20     protected Control createDialogArea(Composite parent) {
21         Composite topComp = new Composite(parent, SWT.NONE);
22         topComp.setLayout(new GridLayout(2, false));
23         new Label(topComp, SWT.NONE).setText("姓名:");
24         nameText = new Text(topComp, SWT.BORDER);
25         nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
26         new Label(topComp, SWT.NONE).setText("年龄:");
27         oldText = new Text(topComp, SWT.BORDER);//省略了只能输入数值的限制
28         oldText.setLayoutData(new GridData(20,-1));
29         
30         new Label(topComp, SWT.NONE).setText("性别:");
31         Composite c = new Composite(topComp, SWT.None);
32         c.setLayout(new RowLayout());
33         ggButton = new Button(c, SWT.RADIO);
34         ggButton.setText("男");
35         mmButton = new Button(c, SWT.RADIO);
36         mmButton.setText("女");
37 
38         if (human != null) {
39             nameText.setText(human.getName() == null ? "" : human.getName());
40             oldText.setText(String.valueOf(human.getOld()));
41             ggButton.setSelection(human.isSex());
42             mmButton.setSelection(!human.isSex());
43         }
44         return topComp;
45     }
46 
47     protected void buttonPressed(int buttonId) {
48         if (buttonId == IDialogConstants.OK_ID) {// 如果单击确定按钮,则将值保存到Human对象中
49             if (human == null)
50                 human = new Human();
51             human.setName(nameText.getText());
52             human.setOld(Integer.parseInt(oldText.getText()));
53             human.setSex(ggButton.getSelection());
54         }
55         super.buttonPressed(buttonId);
56     }
57 }

 

运行结果:

关于JFace的自定义对话框(Dialog类)

点击OK

关于JFace的自定义对话框(Dialog类)

如果是女最后Console最后一行是false.

 

保存对话框的值(IDialogSettings类)

有时候会有这样的状态,记下对话框的状态,当用户再次打开的时候再还原状态,这就需要把对话框的值保存下来,一般是保存到一个文本文件中.Dialog的IDialogSettings类提供了这样的功能,不过用起来比较繁琐,IDialogSetting类本身并不复杂,它仅是提供了对文件操作和设值操作的封装,它生成的文件是一个XML文件.如下所示:

关于JFace的自定义对话框(Dialog类)

保存对话框值的实现代码如下,与MyDialog3相同的代码都省略了,运行示例的客户端是MyDialog4Client.java

MyDialog4.java

  1 import java.io.IOException;
  2 
  3 import org.eclipse.jface.dialogs.Dialog;
  4 import org.eclipse.jface.dialogs.DialogSettings;
  5 import org.eclipse.jface.dialogs.IDialogConstants;
  6 import org.eclipse.jface.dialogs.IDialogSettings;
  7 import org.eclipse.swt.SWT;
  8 import org.eclipse.swt.layout.GridData;
  9 import org.eclipse.swt.layout.GridLayout;
 10 import org.eclipse.swt.layout.RowLayout;
 11 import org.eclipse.swt.widgets.Button;
 12 import org.eclipse.swt.widgets.Composite;
 13 import org.eclipse.swt.widgets.Control;
 14 import org.eclipse.swt.widgets.Label;
 15 import org.eclipse.swt.widgets.Shell;
 16 import org.eclipse.swt.widgets.Text;
 17 
 18 public class MyDialog4 extends Dialog {
 19     private Human human;
 20     private Text nameText;
 21     private Text oldText;
 22     private Button ggButton, mmButton;
 23 
 24     public MyDialog4(Shell parentShell) {
 25         super(parentShell);
 26     }
 27 
 28     public Human getInput() {
 29         return this.human;
 30     }
 31 
 32     public void setInput(Human human) {
 33         this.human = human;
 34     }
 35 
 36     // 在这个方法里构建Dialog中的界面内容
 37     protected Control createDialogArea(Composite parent) {
 38         Composite topComp = new Composite(parent, SWT.NONE);
 39         topComp.setLayout(new GridLayout(2, false));
 40         new Label(topComp, SWT.NONE).setText("姓名:");
 41         nameText = new Text(topComp, SWT.BORDER);
 42         nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 43         new Label(topComp, SWT.NONE).setText("年龄:");
 44         oldText = new Text(topComp, SWT.BORDER);// 省略了只能输入数值的限制
 45         oldText.setLayoutData(new GridData(20, -1));
 46 
 47         new Label(topComp, SWT.NONE).setText("性别:");
 48         Composite c = new Composite(topComp, SWT.None);
 49         c.setLayout(new RowLayout());
 50         ggButton = new Button(c, SWT.RADIO);
 51         ggButton.setText("男");
 52         mmButton = new Button(c, SWT.RADIO);
 53         mmButton.setText("女");
 54 
 55         // 如果没有手动设置初始值,则取出以前保存在文件里的值更新到Human对象
 56         if (human == null)
 57             restoreState();
 58         
 59         if (human != null) {
 60             nameText.setText(human.getName() == null ? "" : human.getName());
 61             oldText.setText(String.valueOf(human.getOld()));
 62             ggButton.setSelection(human.isSex());
 63             mmButton.setSelection(!human.isSex());
 64         }
 65         return topComp;
 66     }
 67 
 68     protected void buttonPressed(int buttonId) {
 69         if (buttonId == IDialogConstants.OK_ID) {// 如果单击确定按钮,则将值保存到Human对象中
 70             if (human == null)
 71                 human = new Human();
 72             human.setName(nameText.getText());
 73             human.setOld(Integer.parseInt(oldText.getText()));
 74             human.setSex(ggButton.getSelection());
 75             saveState();//将Human对象保存到文件
 76         }
 77         super.buttonPressed(buttonId);
 78     }
 79 
 80     // 将Human对象保存到文件
 81     public void saveState() {
 82         if (human == null)
 83             return;
 84         IDialogSettings topSettings = getTopSettings();
 85         IDialogSettings settings = topSettings.getSection("MyDialog4");
 86         if (settings == null)
 87             settings = topSettings.addNewSection("MyDialog4");
 88         settings.put("name", human.getName());
 89         settings.put("old", human.getOld());
 90         settings.put("sex", human.isSex());
 91         try {
 92             topSettings.save("icons/system.xml");
 93         } catch (IOException e) {
 94             e.printStackTrace();
 95         }
 96     }
 97 
 98     // 取出保存的值并更新到Human对象
 99     public void restoreState() {
100         IDialogSettings topSettings = getTopSettings();
101         IDialogSettings settings = topSettings.getSection("MyDialog4");
102         if (settings == null)
103             return;
104         if (human == null)
105             human = new Human();
106         human.setName(settings.get("name"));
107         human.setOld(settings.getInt("old"));
108         human.setSex(settings.getBoolean("sex"));
109     }
110 
111     // 取得*的IDialogSettings
112     public IDialogSettings getTopSettings() {
113         IDialogSettings topSettings = new DialogSettings("system");
114         try {
115             topSettings.load("icons/system.xml");
116         } catch (IOException e) {
117             System.out.println(e.getMessage());
118         }
119         return topSettings;
120     }
121 }

 

MyDialog4Client.java

 1 import org.eclipse.jface.dialogs.IDialogConstants;
 2 import org.eclipse.swt.widgets.Display;
 3 import org.eclipse.swt.widgets.Shell;
 4 
 5 public class MyDialog4Client {
 6     public static void main(String[] args) {
 7         final Display display = Display.getDefault();
 8         final Shell shell = new Shell();
 9         shell.setSize(327, 253);
10         // ---------创建窗口中的其他界面组件-------------
11         // 生成一个Human对象
12         Human human = new Human();
13         human.setName("陈刚");
14         human.setOld(80);
15         human.setSex(true);
16 
17         MyDialog4 dialog = new MyDialog4(shell);
18 //        dialog.setInput(human);
19         if (dialog.open() == IDialogConstants.OK_ID) {// 是否单击“确定”按钮
20             Human o = dialog.getInput(); // 取值
21             if (o == human)
22                 System.out.println("是同一个对象");
23             System.out.println(o.getName());
24             System.out.println(o.getOld());
25             System.out.println(o.isSex());
26         }
27         // -----------------END------------------------
28         shell.dispose();
29         display.dispose();
30     }
31 }

 

运行结果如下:

刚运行如下图:(一开始没有生成system.xml文件,对话框中的内容页是空的)

关于JFace的自定义对话框(Dialog类)

然后输相应的值点击确定值之后在控制台上的输出,并且在对应的路径下生成了对应的system.xml文件:

关于JFace的自定义对话框(Dialog类)

第二次再次启动这个文件的时候(对话框中保留上次的输入的值和system.xml文件中对应的内容信息.):

关于JFace的自定义对话框(Dialog类)

带提示栏的对话框(TitileAreaDialog类)

TitileAreaDialog是Dialog的子类,它在原Dialog界面的顶部加了一条信息提示栏.扩展TitleAreaDialog和扩展Dialog的方法一样,使用方法也相同,示例代码如下:

MyTitleAreaDialog.java

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MyTitleAreaDialog extends TitleAreaDialog {

    public MyTitleAreaDialog(Shell parentShell) {
        super(parentShell);
    }

    protected Control createDialogArea(Composite parent) {
        setTitle("对话框标题");
        setMessage("请在文本框中输入文字");
         setMessage("对话框的说明文字", IMessageProvider.INFORMATION);
        // setErrorMessage("输入错误");
        Composite topComp = new Composite(parent, SWT.BORDER);
        topComp.setLayoutData(new GridData(GridData.FILL_BOTH));
        topComp.setLayout(new RowLayout());
        new Label(topComp, SWT.NONE).setText("请输入:");
        Text text = new Text(topComp, SWT.BORDER);
        text.setLayoutData(new RowData(100, -1));
        return topComp;
    }
}

 

MyTitleAreaDialogClient.java
 1 import org.eclipse.swt.widgets.Display;
 2 import org.eclipse.swt.widgets.Shell;
 3 
 4 public class MyTitleAreaDialogClient {
 5     public static void main(String[] args) {
 6         Display display = Display.getDefault();
 7         Shell shell = new Shell(display);
 8 
 9         new MyTitleAreaDialog(shell).open();
10 
11         display.dispose();
12     }
13 }

 

对应的输出结果:

关于JFace的自定义对话框(Dialog类)

运行结果如上图.代码中对topComp设置了一个GridData.如果不设置,得到的效果图

 关于JFace的自定义对话框(Dialog类)

不设置的GridData的话,面板龟缩在对话框的一角,从这一句也可以知道,createDialogArea方法传入的容器用的是GridLayout布局.

设置信息栏,除了setMessage方法,还有setErrorMessage方法,后者在信息前多加了一个小红叉图标,另外,setMessage方法还可以加式样,如下示例是在信息前加一个警告图标.

setMessage("对话框的说明文字",IMessageProvider.WARNING):

 

共4种式样:

·IMessageProvider.INFORMATION:信息图标.

·IMessageProvider.WARNING:警告图标.

·IMessageProvider.ERROR:错误图标,和setErrorMessage(String str)效果相同.

·IMessageProvider.NONE:什么都没有,和setMessage(String str)效果相同.