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();
}
}
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类的这段代码上:
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();
}
}
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;
}