main
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DialogShell {
public DialogShell() {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
shell.setSize(500, 200);
final Button openDialog = new Button(shell, SWT.PUSH);
openDialog.setText("Click here to rate this book ...");
openDialog.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
MyTitleAreaDialog dialog = new MyTitleAreaDialog(shell);
dialog.open();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DialogShell();
}
}
Title Area dialog:
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
public class MyTitleAreaDialog extends TitleAreaDialog {
public MyTitleAreaDialog(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
// TitleArea中的Title
setTitle("My TitleAreaDialog");
// TitleArea中的Message
setMessage("This is a simple TitleAreaDialog example.");
// TitleArea中的Image
setTitleImage(new Image(null,"icons/Overview4.gif"));
return area;
}
// 按钮,可以重载达到多按钮的效果,或者改变按钮文字
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
createButton(parent, IDialogConstants.PROCEED_ID, IDialogConstants.PROCEED_LABEL, false);
}
// 初始大小
protected Point getInitialSize() {
return new Point(500, 375);
}
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
// Dialog Title
newShell.setText("Test TitleAreaDialog Title");
// Dialog Icon
newShell.setImage(new Image(null,"icons/about.png"));
//newShell.setSize( new Point(800, 375));// 在这儿也可以设置大小
}
}
效果图: