指定JFace.Dialog初始化的位置

作者: dearwolf 发表于:javaeye 原文链接:http://www.javaeye.com/topic/40872
目的1: 打开一个新的 对话框 时,如何设定它和父 对话框 的相对位置?比如在登录 对话框 有一个“创建新帐号”的按钮,用户点击以后,就出现新的对话框用于注册,请问如何能让新的 对话框 和旧 对话框 排列的整齐一些?应该是能设定二者的相对位置吧?

最开始,以为要用 Shell.setLocation来设置,但是对于一个Dialog而言,它的Shell在什么时候才能初始化呢?

我在构造函数里面,configureShell(Shell newShell)方法里面,Control createDialogArea(Composite parent)方法里面都调用过了this.getShell方法想得到当前的Shell,结果都抛出空指针异常....

后来看书发现,应该重写protected Point getInitialLocation(Point initialSize)方法

比如,在最开始的例子中,在第二个对话框中我重写了该方法,代码如下:
  1. protected Point getInitialLocation(Point initialSize) {  
  2.         Point location = new Point(this.getParentShell().getLocation().x  
  3.                 + this.getParentShell().getBounds().width, this  
  4.                 .getParentShell().getLocation().y  
  5.                 + this.getParentShell().getBounds().height  
  6.                 - this.getInitialSize().y);  
  7.         return location;  
  8.     } 
其结果就是两个对话框底部对齐的平行排列:)

目的2: 登陆对话框要记住上次的位置。

想了半天,好像只能用IPreferenceStore来做了,在继承了AbstractUIPlugin的类中放入两个常量: 
  1. public static final String LOGINDIALOG_POSITION_X = "LOGINDIALOG_POSITION_X";  
  2.   
  3. public static final String LOGINDIALOG_POSITION_Y = "LOGINDIALOG_POSITION_Y"
然后重写两个方法: 
  1. @Override  
  2.     protected Point getInitialLocation(Point initialSize) {  
  3.   
  4.         String xposition = preferenceStore  
  5.                 .getString(Peer68TPlugin.LOGINDIALOG_POSITION_X);  
  6.         String yposition = preferenceStore  
  7.                 .getString(Peer68TPlugin.LOGINDIALOG_POSITION_Y);  
  8.         if (xposition == null || yposition == null || xposition == ""  
  9.                 || yposition == "") {  
  10.             return super.getInitialLocation(initialSize);  
  11.         } else {  
  12.             return new Point(Integer.parseInt(xposition), Integer  
  13.                     .parseInt(yposition));  
  14.         }  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean close() {  
  19.         preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_X, this  
  20.                 .getShell().getLocation().x);  
  21.         preferenceStore.setValue(Peer68TPlugin.LOGINDIALOG_POSITION_Y, this  
  22.                 .getShell().getLocation().y);  
  23.         return super.close();  
  24.     } 
大功告成!
org.eclipse.jface.dialogs.Dialog 是 Eclipse JFace 库中的一个类,它提供了一个可定制的对话框窗口,可以用于显示信息、获取用户输入或进行其他交互。Dialog 类实现了一个标准窗口 shell,可以包含任意的 SWT 控件。 使用 Dialog 类创建一个对话框窗口的基本步骤如下: 1. 创建一个继承自 Dialog 类的子类,并实现必要的构造函数和 createDialogArea() 方法,该方法创建对话框的主要内容。 2. 在构造函数中设置对话框的标题和样式,例如是否支持调整大小、是否具有模态行为等。 3. 重写 createContents() 方法,该方法创建对话框的按钮区域和其他控件。 4. 提供必要的 getter 和 setter 方法,以便其他类可以访问对话框中的数据。 5. 如果需要对用户输入进行验证,可以重写 validateInput() 方法。 下面是一个简单的示例代码,演示了如何使用 Dialog 类创建一个简单的对话框窗口: ``` import org.eclipse.jface.dialogs.Dialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; 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 MyDialog extends Dialog { private Text text; private String input; public MyDialog(Shell parentShell) { super(parentShell); setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); setText("My Dialog"); } @Override protected Control createDialogArea(Composite parent) { Composite container = (Composite) super.createDialogArea(parent); GridLayout layout = new GridLayout(2, false); layout.marginRight = 5; layout.marginLeft = 10; container.setLayout(layout); Label lblNewLabel = new Label(container, SWT.NONE); lblNewLabel.setText("Input:"); text = new Text(container, SWT.BORDER); text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); return container; } @Override protected void createButtonsForButtonBar(Composite parent) { createButton(parent, IDialogConstants.OK_ID, "OK", true); createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false); } @Override protected void buttonPressed(int buttonId) { if (buttonId == IDialogConstants.OK_ID) { input = text.getText(); } else { input = null; } super.buttonPressed(buttonId); } @Override protected void configureShell(Shell shell) { super.configureShell(shell); shell.setSize(300, 200); } @Override protected boolean isResizable() { return true; } public String getInput() { return input; } public void setInput(String input) { this.input = input; } @Override protected void okPressed() { validateInput(); super.okPressed(); } private void validateInput() { if (text.getText().isEmpty()) { setErrorMessage("Input must not be empty"); } else { setErrorMessage(null); } } } ``` 在这个示例中,我们创建了一个名为 MyDialog 的子类,它包含一个文本框用于输入文本。我们在构造函数中设置了对话框的标题和样式,并在 createDialogArea() 方法中创建了文本框。我们还重写了 createButtonsForButtonBar() 方法以创建 OK 和 Cancel 按钮,并重写了 buttonPressed() 方法以处理按钮点击事件。最后,我们提供了 getter 和 setter 方法以便其他类可以访问对话框中的数据,并重写了 okPressed() 方法和 validateInput() 方法以验证用户输入。 通过调用 MyDialog.open() 方法,我们可以在 Eclipse 应用程序中显示这个对话框窗口: ``` MyDialog dialog = new MyDialog(shell); if (dialog.open() == Window.OK) { String input = dialog.getInput(); // Do something with the input } ``` 在这个示例中,我们创建了一个 MyDialog 对象,并调用其 open() 方法以显示对话框窗口。如果用户单击 OK 按钮,我们可以通过调用 getInput() 方法获取用户输入的文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值