目的1:
打开一个新的对话框时,如何设定它和父对话框的相对位置?比如在登录对话框有一个“创建新帐号”的按钮,用户点击以后,就出现新的对话框用于注册,请问如何能让新的对话框和旧对话框排列的整齐一些?应该是能设定二者的相对位置吧?
最开始,以为要用
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;
- }
其结果就是两个对话框底部对齐的平行排列:)
目的2: 登陆对话框要记住上次的位置。
想了半天,好像只能用IPreferenceStore来做了,在继承了AbstractUIPlugin的类中放入两个常量:
- public static final String LOGINDIALOG_POSITION_X = "LOGINDIALOG_POSITION_X";
-
- 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();
- }
大功告成!
分享到:
- 2006-11-08 15:23
- 浏览 3522
- 评论(5)
- 论坛回复 / 浏览 (5 / 6715)
- 查看更多
相关推荐
Eclipse Rich Client Application 开发自学教程 For The Eclipser salever 2011-3-28 根据最新版本的Eclipse 3.6 重新编写,每章都可独立于其他章节,内附的代码均可直接运行, 适合Eclipse 开发者参考。...
7.5.4 设置控件的相对位置 115 7.6 StackLayout(堆栈式布局) 115 7.7 自定义布局管理器 117 7.7.1 布局的基本原理 117 7.7.2 布局计算的常用方法 118 7.7.3 自定义布局类(BorderLayout) 119 7.8 ...
4. **工作流和生命周期管理**:掌握Eclipse插件的启动、初始化、激活和停用过程,以及事件监听和处理。 5. **界面设计**:通过布局管理器(Layout Manager)实现界面的自适应和美观设计,以及使用SWT和JFace的...
- **初始化资源和服务**:Activator类负责初始化插件中的资源和服务,并提供插件API供其他组件调用。 3. **plugin.xml与org.eclipse.ui.actionSets扩展点** - **定义扩展点**:通过`plugin.xml`文件定义扩展点,如...
- **Activator类**:用于初始化插件,处理插件生命周期中的事件。 - **plugin.xml**:定义插件元数据和扩展点。 - **扩展点**:如`org.eclipse.ui.actionSets`用于定义菜单和工具栏等用户界面元素。 - **...
- **实现SampleEditor类**:在代码示例中,`SampleEditor`继承自`EditorPart`,实现了编辑器的基本功能,包括初始化、保存等。 ##### 5.4 调用编辑器 - **调用流程**:介绍如何在应用中调用编辑器,通常涉及到编辑...