僅僅是使用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();
運行結果:
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,其譜系圖如下所示:
可見Composite是Control的子類,所以上面的程序中返回topComp對象也是可以的.
運行結果:
3.
改變對話框的式樣:
上面的運行結果在對話框右上角只有一個關閉按鈕,如果想讓器其有最小化,最大化按鈕,並且窗口可用鼠標改變大小,則只需要再MyDialog類中改寫父類的getShellStyle方法即可,如果讓此方法只返回SWT.NONE式樣,則得到如下的結果.代碼如下:
如果是這個設置return super.getShellStyle(); 或者是 return super.getShellStyle() |SWT.NONE;
對應的輸入框如下:
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;
對應的輸入框如下:
4.定制對話框的按鈕:
如果想在對話框中增加按鈕,則需要改寫父類的initializeBounds方法,如果還想改變"確定,取消"兩個按鈕的名稱,則需要再改寫父類的createButton方法,讓它空實現,
如下圖:
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 }
運行結果:
對話框的設置與取值:
在實際開發中,經常需要設置對話框中的組件的初始值,並在單擊對話框的"確定"按鈕后,取出對話框中的值進行相應的處理.實現對話框設置取值的關鍵是為各組件創建對應的變量來保存值.如下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 }
運行結果:
點擊OK按鈕:
也許有讀者會問:為什么要在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,在控制台拋出的錯誤:
因為在執行dialog. open()方法之前,對話框界面上的組件還沒有創建,text文本框對象是空值,因此,dialog.text.setText()這一句會報出NullPointerException異常.
當單擊"確定"按鈕對話框退出,這時對話框中的組件將被銷毀,所以對text做取值操作 也會包NullPointerException異常.正是由於這些原因,才不得不建立一個String類型的實例變量來保存文本框值做中轉.
封裝對話框中的數據到一個數據類:
往往對話框中會有很多的組件,如果沒一個組件都需要一個變量來保存它的值,這樣就會有很多變量.這樣,對話框類就需要提供一個變量一對Setter/Getter方法.類的方法數量會暴漲,從而對Dialog類嘗試了嚴重的污染.流行的MVC模式就是 要把界面和數據盡可能的剝離開來,因此應該把對話框的數據封裝到一個數據類中.
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 }
運行結果:
點擊OK
如果是女最后Console最后一行是false.
保存對話框的值(IDialogSettings類)
有時候會有這樣的狀態,記下對話框的狀態,當用戶再次打開的時候再還原狀態,這就需要把對話框的值保存下來,一般是保存到一個文本文件中.Dialog的IDialogSettings類提供了這樣的功能,不過用起來比較繁瑣,IDialogSetting類本身並不復雜,它僅是提供了對文件操作和設值操作的封裝,它生成的文件是一個XML文件.如下所示:
保存對話框值的實現代碼如下,與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文件,對話框中的內容頁是空的)
然后輸相應的值點擊確定值之后在控制台上的輸出,並且在對應的路徑下生成了對應的system.xml文件:
第二次再次啟動這個文件的時候(對話框中保留上次的輸入的值和system.xml文件中對應的內容信息.):
帶提示欄的對話框(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 }
對應的輸出結果:
運行結果如上圖.代碼中對topComp設置了一個GridData.如果不設置,得到的效果圖
不設置的GridData的話,面板龜縮在對話框的一角,從這一句也可以知道,createDialogArea方法傳入的容器用的是GridLayout布局.
設置信息欄,除了setMessage方法,還有setErrorMessage方法,后者在信息前多加了一個小紅叉圖標,另外,setMessage方法還可以加式樣,如下示例是在信息前加一個警告圖標.
setMessage("對話框的說明文字",IMessageProvider.WARNING):
共4種式樣:
·IMessageProvider.INFORMATION:信息圖標.
·IMessageProvider.WARNING:警告圖標.
·IMessageProvider.ERROR:錯誤圖標,和setErrorMessage(String str)效果相同.
·IMessageProvider.NONE:什么都沒有,和setMessage(String str)效果相同.