Eclipse
提供了较完整、标准的消息对话组件,下面介绍一下MessageDialog
的使用。
简介
MessageDialog
位于org.eclipse.jface.dialogs
包中,继承自IconAndMessageDialog
。MessageDialog官方API
字段
类型 | 字段名 | 值 | 描述 |
---|---|---|---|
static final int | NONE | 0 | 没有图标的对话框 |
static final int | ERROR | 1 | 带有错误(error)图标,或简单按钮的消息提示框 |
static final int | INFORMATION | 2 | 带有信息(info)图标,或简单按钮的消息提示框 |
static final int | QUESTION | 3 | 带有问号(question)图标,或 Yes/No 按钮的消息提示框 |
static final int | WARNING | 4 | 带有告警(WARNING)图标,或简单按钮的消息提示框 |
static final int | CONFIRM | 5 | 带有确认( CONFIRM)图标,或带有 OK/Cancel 按钮的消息提示框 |
static final int | QUESTION_WITH_CANCEL | 6 | 带有信息( QUESTION_WITH_CANCEL)图标,或Yes/No/Cancel 按钮的消息提示框 |
构造方法
MessageDialog(Shell parentShell, String dialogTitle, Image dialogTitleImage, String dialogMessage, int dialogImageType, String[] dialogButtonLabels, int defaultIndex)
内置方法
protected void buttonPressed(int buttonId)
Notifies that this dialog’s button with the given id has been pressed.protected void configureShell(Shell shell)
Configures the given shell in preparation for opening this window in it.protected Button createButton(Composite parent, int id, String label, boolean defaultButton)
Creates a new button with the given id.protected void createButtonsForButtonBar(Composite parent)
Adds buttons to this dialog’s button bar.protected Control createCustomArea(Composite parent)
Creates and returns the contents of an area of the dialog which appears below the message and above the button bar.protected Control createDialogArea(Composite parent)
This implementation of the Dialog framework method creates and lays out a composite and calls createMessageArea and createCustomArea to populate it.protected boolean customShouldTakeFocus()
Return whether or not we should apply the workaround where we take focus for the default button or if that should be determined by the dialog.protected Button getButton(int index)
Gets a button in this dialog’s button bar.protected String[] getButtonLabels()
An accessor for the labels to use on the buttons.protected int getDefaultButtonIndex()
An accessor for the index of the default button in the button array.Image getImage()
Returns the image to display beside the message in this dialog.protected int getMinimumMessageWidth()
Returns the minimum message area width in pixels This determines the minimum width of the dialog.protected void handleShellCloseEvent()
Handle the shell close.int open()
Opens this message dialog, creating it first if it has not yet been created.- ⭐️
static boolean open(int kind, Shell parent, String title, String message, int style)
Convenience method to open a simple dialog as specified by the kind flag. - ⭐️
static boolean openConfirm(Shell parent, String title, String message)
Convenience method to open a simple confirm (OK/Cancel) dialog. - ⭐️
static void openError(Shell parent, String title, String message)
Convenience method to open a standard error dialog. - ⭐️
static void openInformation(Shell parent, String title, String message)
Convenience method to open a standard information dialog. - ⭐️
static boolean openQuestion(Shell parent, String title, String message)
Convenience method to open a simple Yes/No question dialog. - ⭐️
static void openWarning(Shell parent, String title, String message)
Convenience method to open a standard warning dialog. - ⭐️
protected void setButtonLabels(String[] buttonLabels)
A mutator for the button labels. - ⭐️
protected void setButtons(Button[] buttons)
A mutator for the array of buttons in the button bar.
其中带⭐️的方法开箱即用,使用也比较频繁,示例见下一章。具有较强业务的消息对话框用户可以根据构造方法构造,最后调用.open()
打开。
简单使用
Shell shell = Display.getCurrent().getActiveShell();
MessageDialog.openInformation(shell, "提示", "静态检查未通过,请查看静态检查结果并处理问题");
MessageDialog.openError(shell, "错误消息", "一条错误的消息");
MessageDialog.openQuestion(shell, "询问", "询问一下,这是询问消息么?");
MessageDialog.openWarning(shell, "警告", "请别再随意造轮子,我够用了!!!");
MessageDialog.openConfirm(shell, "确认", "你没有造其他轮子吧?");
- 信息对话框
- 错误对话框
- 询问对话框
- 警告对话框
- 确认对话框