一、在用
eclipse
进行图形编程时,对话框是经常要用到的控件之一,如果对话框继承
Jface
的
Dialog
类就会在底部自动生成
Ok
和
Cancel
按钮,通常情况我们都需要这两个按钮,但在一些特殊情况,我们又不得不考虑用户的特殊需求,有时就需要去掉底部的这两个按钮。那怎么去掉这两个按钮呢
1)
一个继承自
Jface
的
Dialog
的对话框,主要由两部分组成,
一个是:
protected
Control createDialogArea(Composite parent) {
Composite container = (Composite)
super
.createDialogArea(parent);
//
可以在这里添加代码以增加新的组件
return
container;
}
说明:该函数主要是生成对话框的主面板上的控件
另一个是:
protected
void
createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.
OK_ID
, IDialogConstants.
OK_LABEL
,
true
);
createButton(parent, IDialogConstants.
CANCEL_ID
,
IDialogConstants.
CANCEL_LABEL
,
false
);
}
说明:该函数主要是生成底部的
Ok
和
Cnacel
按钮
2)
Dialog.java
中有一个
createContents
函数
protected
Control createContents(Composite parent) {
// create the top level composite for the dialog
Composite composite =
new
Composite(parent, 0);
GridLayout layout =
new
GridLayout();
layout.
marginHeight
= 0;
layout.
marginWidth
= 0;
layout.
verticalSpacing
= 0;
composite.setLayout(layout);
composite.setLayoutData(
new
GridData(GridData.
FILL_BOTH
));
applyDialogFont(composite);
// initialize the dialog units
initializeDialogUnits(composite);
// create the dialog area and button bar
dialogArea
= createDialogArea(composite);
buttonBar
= createButtonBar(composite);//
注释掉此行即可去掉底部的
buttonBar
return
composite;
}
二、打开一个自己的
Dialog
窗口(命名为
ChildDialog
)的具体过程是什么样子的呢?让我们用调试跟踪的方法来具体看一下:
1)
ChildDialog dialog = new ChildDialog(null);
首先进入到
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$
}
2
)随后进入
static IDialogBlockedHandler blockedHandler = new IDialogBlockedHandler()
主要是创建一个阻塞的句柄,就是通常所说的模式对话框
public static IDialogBlockedHandler blockedHandler = new IDialogBlockedHandler() {
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.IDialogBlockedHandler#clearBlocked()
*/
public void clearBlocked() {
// No default behaviour
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.IDialogBlockedHandler#showBlocked(org.eclipse.core.runtime.IProgressMonitor,
* org.eclipse.core.runtime.IStatus, java.lang.String)
*/
public void showBlocked(IProgressMonitor blocking,
IStatus blockingStatus, String blockedName) {
//No default behaviour
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.IDialogBlockedHandler#showBlocked(org.eclipse.swt.widgets.Shell,
* org.eclipse.core.runtime.IProgressMonitor,
* org.eclipse.core.runtime.IStatus, java.lang.String)
*/
public void showBlocked(Shell parentShell, IProgressMonitor blocking,
IStatus blockingStatus, String blockedName) {
//No default behaviour
}
};
3
)随后进入
ChildDialog
的初始化函数,这里只是简单的调用
Dialog
的初始化函数,生成一个
dialog
的
instance
,注意各个界面控件元素并没有在生成。
public ChildDialog(Shell parentShell) {
super(parentShell);
}
4
)随后进入
Window.java
的
open
函数
public int open() {
if (shell == null || shell.isDisposed()) {
shell = null;
// create the window
create();//
进入到
5
)中的
create
函数
}
// limit the shell size to the display size
constrainShellSize();
// open the window
shell.open();
// run the event loop if specified
if (block)
runEventLoop(shell);
return returnCode;
}
说明:返回
returnCode
就是指定是
Ok
按钮还是
Cnacel
按钮被按下了,这样一个完整的对话框打开过程就完成了
5
)
Window.java
中的
create()
函数
public void create() {
shell = createShell();
contents = createContents(shell);//
注意这个函数,这就是在第一部分的
2
)小节提到的函数,用于构造面板
//initialize the bounds of the shell to that appropriate for the
// contents
initializeBounds();
}
说明:只有在
open
()之后界面中的控件元素才会去创建