📄 messagebox.java
字号:
package com.studentmanage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.swtdesigner.SWTResourceManager;
/**
* 自己定义的一个对话框程序。
* 使用
* MessageBox ms=new MessageBox(new Shell(),"title","info");
ms.open();
if(ms.isClickedOk)
* todoSomething
* @author 刘贵学
*
*/
public class MessageBox extends Dialog
{
protected Object result;
protected Shell shell;
// 如果单击确定返回true
public boolean isClickedOk = false;
private Label lbInfo;
/**
* Create the dialog
*
* @param parent
* @param style
*/
public MessageBox(Shell parent, int style)
{
super(parent, style);
createContents();
}
/**
* Create the dialog
*
* @param parent
*/
public MessageBox(Shell parent)
{
this(parent, SWT.NONE);
createContents();
}
public MessageBox(Shell parent,String title,String info)
{
this(parent, SWT.NONE);
setTitle(title);
setInfo(info);
}
public MessageBox(Shell parent,String info)
{
this(parent, SWT.NONE);
setInfo(info);
}
// 设置标题
public void setTitle(String title)
{
shell.setText(title);
}
public void setInfo(String info)
{ lbInfo.setText(info);
}
/**
* Open the dialog
*
* @return the result
*/
// 打开对话框
public Object open()
{
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents()
{
shell = new Shell(getParent(), SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
shell.setSize(466, 163);
shell.setMaximized(false);
shell.setMinimized(false);
final Button btnCancel = new Button(shell, SWT.NONE);
btnCancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0)
{
shell.close();
}
});
btnCancel.setText("取消");
btnCancel.setBounds(120, 101, 71, 25);
final Button btnOk = new Button(shell, SWT.NONE);
btnOk.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0)
{
isClickedOk = true;
shell.close();
}
});
btnOk.setBounds(264, 101, 66, 25);
btnOk.setText("确定");
lbInfo = new Label(shell, SWT.CENTER | SWT.WRAP);
lbInfo.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
lbInfo.setBounds(44, 22, 369, 60);
//
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -