DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          swt/jface 自定義 Dialog

          自定義Dialog很簡單,下邊我們來一步步實現自定義Dialog
          一、寫一個類,繼承自Dialog
          import org.eclipse.jface.dialogs.Dialog;
          import org.eclipse.swt.widgets.Shell;

          public class TestDialog extends Dialog {

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

          }

          好了,寫好了,如何運行呢?
          再寫一個類:
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;

          public class Test {
              
          public static void main(String[] args) {
                  Shell shell 
          = new Shell();
                  TestDialog td 
          = new TestDialog(shell);
                  td.setBlockOnOpen(
          true);
                  td.open();
                  Display.getCurrent().dispose();
              }

          }

          好了運行一下看到效果了吧,帶有兩個button.
          二、看到上邊的代碼是否會想到別的呢?為什么要再寫一個類來運行Dialog,不能在內部寫個main方法嗎?
          我們來試一下:
          方法一:參考Jface hello world的的寫法:
          import org.eclipse.jface.dialogs.Dialog;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;

          public class TestDialog extends Dialog {

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

              
          public static void main(String[] args) {
                  TestDialog td 
          = new TestDialog(new Shell());
                  td.setBlockOnOpen(
          true);
                  td.open();
                  Display.getCurrent().dispose();
              }

          }


          運行一下看看什么效果,提示找不到main方法。且打印出如下Exception
          java.lang.ExceptionInInitializerError
          Caused by: java.lang.NullPointerException
           at org.eclipse.jface.resource.JFaceResources.getResources(JFaceResources.java:184)
           at org.eclipse.jface.resource.JFaceResources.getImageRegistry(JFaceResources.java:310)
           at org.eclipse.jface.dialogs.Dialog.<clinit>(Dialog.java:211)
          Exception in thread "main"
          為什么呢?我們是有main方法的,跟進Exception去看看吧。
          發現問題出現在Dialog類的這段代碼上:
          static {
                  ImageRegistry reg 
          = JFaceResources.getImageRegistry();
                  reg.put(DLG_IMG_MESSAGE_INFO, ImageDescriptor.createFromFile(
                          Dialog.
          class"images/message_info.gif")); //$NON-NLS-1$
                  reg.put(DLG_IMG_MESSAGE_WARNING, ImageDescriptor.createFromFile(
                          Dialog.
          class"images/message_warning.gif")); //$NON-NLS-1$
                  reg.put(DLG_IMG_MESSAGE_ERROR, ImageDescriptor.createFromFile(
                          Dialog.
          class"images/message_error.gif")); //$NON-NLS-1$
              }

          原來在靜態代碼塊上出現了Exception,造成在運行main函數之前就退出了。所以才說沒有main函數。
          我們知道classload在加載一個類的時候,對于靜態代碼塊會逐行執行,按照出現的先后順序。同時父類的靜態代碼塊一定比子類的先執行。因為
          在load子類之前會先load父類。這就是為什么hello world中不會出現問題,這里會出現問題的原因。因為Dialog比ApplicationWindow多了這段靜態代碼。

          繼續追下去為什么這段代碼會出現空指針異常呢,原來這段代碼依賴于new Shell()必須先運行。而我們的new Shell()寫在main方法里邊,肯定是在加載類完成后才能運行的。所以在類內部直接寫個main方法是不行的。只能單獨寫個類來調用。
          方法二:
          單獨寫個類如下:

          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;

          public class Test {
              
          public static void main(String[] args) {
                  TestDialog td 
          = new TestDialog(new Shell());
                  td.setBlockOnOpen(
          true);
                  td.open();
                  Display.getCurrent().dispose();
              }

          }

          依然是不行的,報同樣的錯誤,為什么?仔細看一下,我們把new Shell()寫在構造函數的參數里,其實范了和剛才同樣的錯誤。所以單獨提出new Shell(),寫在構造函數之前。就得到了文章開始的Test類。平時我們使用的時候為什么不出這個問題呢?因為我們平時使用的時候Dialog從里不是單獨存在的,在之前shell早被構造過了。反而是demo更容易出這個問題。
          好了,上邊只是個小插曲,繼續我們的自定義Dialog.
          三、去掉兩個按鈕
          雖然Dialog天生帶的兩個按鈕不錯,但我們有的時候并不想要這兩個按鈕,怎么辦,如何去掉它?
          簡單,只要我們覆蓋父類的createButtonsForButtonBar這個方法就可以了,覆寫這個方法,里邊什么也不寫
          protected void createButtonsForButtonBar(Composite parent) {
              }

          看一下按鈕消失了。
          四、加入右上角的最大化和關閉
          覆寫父類的這個方法:
          protected int getShellStyle(){
                  
          return super.getShellStyle()|SWT.RESIZE|SWT.MAX;
              }

          五、改變Dialog的大小
          覆寫這個方法:
          protected Point getInitialSize(){
                  
          return new Point(300,400);//300是寬400是高
              }
          六、加入自己的控件
          覆寫createDialogArea方法
          protected Control createDialogArea(Composite parent) {
                  Composite container 
          = (Composite) super.createDialogArea(parent);
                  container.setLayout(
          new RowLayout());
                  text 
          = new Text(container, SWT.BORDER);
                  text.setLayoutData(
          new RowData(100,-1));
                  
          return container;
              }

          這里使用了RowLayout
          七、加入自己的按鈕
          覆寫initializeBounds
          protected void initializeBounds(){
                  Composite comp 
          = (Composite)getButtonBar();
                  
          super.createButton(comp, IDialogConstants.OK_ID, "完成"true);
              }
          好了這里自定義Dialog完成了,然后根據你的需要再Dialog中添加更負載的控件,更多的按鈕。最后目的當然是從Dialog取道數值。
          八、帶提示框的Dialog
          使用方法和前邊相同,不同的是不是繼承自Dialog而是繼承自TitleAreaDialog,然后在createDialogArea中加入兩行
          setTitle("標題");
          setMessage(
          "提示信息")
          //setMessage可以加上圖片,加入的辦法是setMessage("提示信息",IMessageProvider.WARNING);如果想加入其他的圖片,調用相應的常量。

          source下載:http://www.aygfsteel.com/Files/dreamstone/jface-dialog.rar

          posted on 2007-08-09 17:42 dreamstone 閱讀(6248) 評論(0)  編輯  收藏 所屬分類: SWT和插件開發

          主站蜘蛛池模板: 台湾省| 秭归县| 玛沁县| 仙居县| 喀喇沁旗| 朝阳县| 洛南县| 进贤县| 双柏县| 枣强县| 太湖县| 黄浦区| 都江堰市| 临夏县| 玛沁县| 白河县| 喀喇沁旗| 安吉县| 平阳县| 南安市| 扬州市| 汶上县| 会昌县| 宜兴市| 凤山市| 夏河县| 吉首市| 岳西县| 浪卡子县| 陆河县| 灯塔市| 岐山县| 台南县| 通江县| 聂拉木县| 庄河市| 兰考县| 邓州市| 武冈市| 泸西县| 习水县|