在button的事件处理方法里面添加:
JoptionPanel.showMessageDialog 就是用来弹出窗口的。还有其他show方法,可以看看源码怎么用。
用来打开对话框的窗口
package com.wojiushiwoltq;
import org.eclipse.swt.SWT;
/**
* 类说明
*
* @author WoJiuShiWoLtq
* @version 1.0
* @Date 2011 2011-4-22 下午04:50:58
*
*/
public class Test {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Test window = new Test();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FormLayout());
Button button = new Button(composite,SWT.NONE);
{
FormData data = new FormData();
data.top = new FormAttachment(0, 10);
data.left=new FormAttachment(0, 10);
button.setLayoutData(data);
button.setText("按钮");
}
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
new MyDialog(null).open();
}
});
}
}
对话窗口
package com.wojiushiwoltq;
import org.eclipse.jface.dialogs.Dialog;
/**
* 类说明
*
* @author WoJiuShiWoLtq
* @version 1.0
* @Date 2011 2011-4-22 下午04:56:16
*
*/
public class MyDialog extends Dialog {
/**
* Create the dialog.
* @param parentShell
*/
public MyDialog(Shell parentShell) {
super(parentShell);
}
/**
* Create contents of the dialog.
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
return container;
}
/**
* Create contents of the button bar.
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(450, 300);
}
}
要在对话框里添加控件就在createDialogArea方法里加就可以了!