目的1:打開一個新的對話框時,如何設定它和父對話框的相對位置?比如在登錄對話框有一個“創建新帳號”的按鈕,用戶點擊以后,就出現新的對話框用于注冊,請問如何能讓新的對話框和舊對話框排列的整齊一些?應該是能設定二者的相對位置吧?
最開始,以為要用Shell.setLocation來設置,但是對于一個Dialog而言,它的Shell在什么時候才能初始化呢?
我 在構造函數里面,configureShell(Shell newShell)方法里面,Control createDialogArea(Composite parent)方法里面都調用過了this.getShell方法想得到當前的Shell,結果都拋出空指針異常....
后來看書發現,應該重寫protected Point getInitialLocation(Point initialSize)方法
比如,在最開始的例子中,在第二個對話框中我重寫了該方法,代碼如下:
其結果就是兩個對話框底部對齊的平行排列:)
目的2: 登陸對話框要記住上次的位置。
想了半天,好像只能用IPreferenceStore來做了,在繼承了AbstractUIPlugin的類中放入兩個常量:
然后重寫兩個方法:
大功告成!
最開始,以為要用Shell.setLocation來設置,但是對于一個Dialog而言,它的Shell在什么時候才能初始化呢?
我 在構造函數里面,configureShell(Shell newShell)方法里面,Control createDialogArea(Composite parent)方法里面都調用過了this.getShell方法想得到當前的Shell,結果都拋出空指針異常....
后來看書發現,應該重寫protected Point getInitialLocation(Point initialSize)方法
比如,在最開始的例子中,在第二個對話框中我重寫了該方法,代碼如下:
protected?Point?getInitialLocation(Point?initialSize)?{
????????Point?location?=?new?Point(this.getParentShell().getLocation().x
????????????????+?this.getParentShell().getBounds().width,?this
????????????????.getParentShell().getLocation().y
????????????????+?this.getParentShell().getBounds().height
????????????????-?this.getInitialSize().y);
????????return?location;
????}
????????Point?location?=?new?Point(this.getParentShell().getLocation().x
????????????????+?this.getParentShell().getBounds().width,?this
????????????????.getParentShell().getLocation().y
????????????????+?this.getParentShell().getBounds().height
????????????????-?this.getInitialSize().y);
????????return?location;
????}
其結果就是兩個對話框底部對齊的平行排列:)
目的2: 登陸對話框要記住上次的位置。
想了半天,好像只能用IPreferenceStore來做了,在繼承了AbstractUIPlugin的類中放入兩個常量:
????public?static?final?String?LOGINDIALOG_POSITION_X?=?"LOGINDIALOG_POSITION_X";
????public?static?final?String?LOGINDIALOG_POSITION_Y?=?"LOGINDIALOG_POSITION_Y";
????public?static?final?String?LOGINDIALOG_POSITION_Y?=?"LOGINDIALOG_POSITION_Y";
然后重寫兩個方法:
????@Override
????protected?Point?getInitialLocation(Point?initialSize)?{
????????String?xposition?=?preferenceStore
????????????????.getString(Peer68TPlugin.LOGINDIALOG_POSITION_X);
????????String?yposition?=?preferenceStore
????????????????.getString(Peer68TPlugin.LOGINDIALOG_POSITION_Y);
????????if?(xposition?==?null?||?yposition?==?null || xposition == ""
??? ??? ??? ??? || yposition == "")?{
????????????return?super.getInitialLocation(initialSize);
????????}?else?{
????????????return?new?Point(Integer.parseInt(xposition),?Integer
????????????????????.parseInt(yposition));
????????}
????}
????@Override
????public?boolean?close()?{
????????preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_X,?this
????????????????.getShell().getLocation().x);
????????preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_Y,?this
????????????????.getShell().getLocation().y);
????????return?super.close();
????}
????protected?Point?getInitialLocation(Point?initialSize)?{
????????String?xposition?=?preferenceStore
????????????????.getString(Peer68TPlugin.LOGINDIALOG_POSITION_X);
????????String?yposition?=?preferenceStore
????????????????.getString(Peer68TPlugin.LOGINDIALOG_POSITION_Y);
????????if?(xposition?==?null?||?yposition?==?null || xposition == ""
??? ??? ??? ??? || yposition == "")?{
????????????return?super.getInitialLocation(initialSize);
????????}?else?{
????????????return?new?Point(Integer.parseInt(xposition),?Integer
????????????????????.parseInt(yposition));
????????}
????}
????@Override
????public?boolean?close()?{
????????preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_X,?this
????????????????.getShell().getLocation().x);
????????preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_Y,?this
????????????????.getShell().getLocation().y);
????????return?super.close();
????}
大功告成!