IntelliJ平台提倡使用 非模式 框来给用户提示错误或各种需要注意的信息。
1 使用各种提醒
1.1 对话框(Dialogs)
当需要用户确认处理时,可以使用 模态 对话框来处理,如下:
自定义对话框 :
先继承 DialogWrapper 类
public class SampleDialogWrapper extends DialogWrapper {public SampleDialogWrapper() {super(true); // use current window as parentinit();setTitle("学习 DialogWrapper");}/*** 自定义提示组件** @return JComponent*/@Nullable@Overrideprotected JComponent createCenterPanel() {JPanel dialogPanel = new JPanel(new BorderLayout());JLabel label = new JLabel("Learn");label.setPreferredSize(new Dimension(100, 100));dialogPanel.add(label, BorderLayout.CENTER);return dialogPanel;}}
调用显示:
public class LearnDialog extends AnAction {@Overridepublic void actionPerformed(@NotNull AnActionEvent anActionEvent) {// 显示 自定义 对话框boolean isOk = MyDialogLearn.showCustomDialog();if(isOk){// 显示系统自带的 对话框Messages.showInputDialog((Project) null, "Enter prefix", "Learn", Messages.getInformationIcon());}}}
其中,第5行是调用系统默认的的对话框。这里,我用按钮来进行触发
效果:
1.2 编辑器提示(Editor Hints)
如果需要在 文件Tab 中提示用户时,可以使用这个提示,如下:
创建实例 :
public class MyEditorHintsLearn {/*** 显示 Tab提示*/public static void showEditorHints(AnActionEvent e) {final Editor editor = e.getData(CommonDataKeys.EDITOR);HintManager.getInstance().showErrorHint(editor, "提醒一下!");}}
这里的第7行,需要打开 编辑器 ,不然会报错!!!
进行调用 :
public class LearnEditorHints extends AnAction {@Overridepublic void actionPerformed(@NotNull AnActionEvent anActionEvent) {MyEditorHintsLearn.showEditorHints(anActionEvent);}}
1.3 提醒框(Top-Level Notifications)
如果不需要用户必须处理这个通知消息,可以使用 悬浮框 ,它包含以下特点:
- 支持Application或Project级别
- 支持
Settings或Appearance and Behavior或Notifications几种不同的通知类型 - 支持自定义
NotificationGroup来显示
使用方式:
NotificationGroup (2020.3 and later) 如下:
1.先在 plugin.xml 中进行注册:
<extensions defaultExtensionNs="com.intellij"><notificationGroup id="Custom Notification Group" displayType="BALLOON" key="notification.group.name"/></extensions>
2.代码提醒部分:
public class MyNotifier {public static void notifyError(@Nullable Project project, String content) {NotificationGroupManager.getInstance().getNotificationGroup("Custom Notification Group").createNotification(content, NotificationType.ERROR).notify(project);}}
NotificationGroup (Pre-2020.3) 如下:
public class MyNotifier {private static final NotificationGroup NOTIFICATION_GROUP =new NotificationGroup("Custom Notification Group", NotificationDisplayType.BALLOON, true);public static void notifyError(@Nullable Project project, String content) {NOTIFICATION_GROUP.createNotification(content, NotificationType.ERROR).notify(project);}}
2 各种提醒框
IntelliJ平台提供了4种
- 模式提醒框(Alert):必须经用户确认关掉
- 横幅条(Banner):需用户确认处理,但无须立即处理
- 悬浮框(Balloon):无须特定处理
- 工具窗悬浮框(Tool window balloon):无须特定处理
在使用这几类提醒框时,我们要考虑2个因素:
- 用户处理方式(User action):必须立即处理?or 必须但可以不立即处理?or 不需要处理?
- 提醒位置(Context of trigger):文件Tab页?or 工具窗?or 其他?
汇聚成下面这个表格:
| 用户处理方式 | 提醒位置 | 类型 | 例子 |
|---|---|---|---|
| 必须立即处理 | 全部 | 模式提醒框(Alert) | - 重启IntelliJ - 在新窗口打开项目 - 当重命名方法时,产生冲突 - 使用某个功能时,需要一个依赖 |

(图片来源: 出处) |
| 必须,但无须立即处理 | 文件Tab
工具窗 | 横幅条(Banner) |
- 给项目设置SDK
- 使用Gradle进行同步

(图片来源:出处) |
| 非必须 | 工具窗 | 工具窗悬浮框 |
- 任务完成的状态

(图片来源:出处) |
| | 除了文件Tab、工具窗悬浮框 | 手动处理的悬浮框 |
- IDE和插件更新

(图片来源:出处) |
| | | 定时关闭的悬浮框 |
- 模块导入
- 发现新的框架

(图片来源:出处) |
如果要显示异常,可以使用 文件Tab的 横幅条 来显示
