插件开发之用户界面组件

2021-08-03 插件开发

IDEA的右下角通知以及对话框和表单控件的开发。

# 1.通知

<extensions defaultExtensionNs="com.intellij">
    <notificationGroup id="MyFirstNotify" displayType="BALLOON"/>
</extensions>
 NotificationGroupManager.getInstance().getNotificationGroup("MyFirstNotify")
                .createNotification("测试", NotificationType.INFORMATION)
                .notify(e.getProject());

# 2.对话框

第一步:创建提示框

public class SampleDialogWrapper extends DialogWrapper {
    protected SampleDialogWrapper(String title) {
        super(true);
        setTitle(title); // 标题
        init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel panel = new JPanel();
        JLabel jLabel = new JLabel("我是内容部分");
        panel.add(jLabel);
        return panel;
    }
}

第二步:显示

SampleDialogWrapper dialogWrapper = new SampleDialogWrapper("启动提示");
dialogWrapper.show();

第三步:重写底部按钮

@Override
protected JComponent createSouthPanel() {
    JPanel panel = new JPanel(new BorderLayout());
    JButton btn = new JButton("点击确定"); // 按钮名称
    btn.addActionListener(e -> {
        // TODO 按钮点击后的事件
    });
    panel.add(btn);
    return panel;
}

# 3.表单控件

输入框

// 核心代码
EditorTextField tfContent = new EditorTextField();
tfContent.setPlaceholder("请输入笔记内容"); // 提示信息
tfContent.setPreferredSize(new Dimension(200,100)); // 输入框大小
// 示例
protected @Nullable JComponent createCenterPanel() {
    JPanel panel = new JPanel(new BorderLayout()); // 区域布局,分为东南西北
    tfTitle = new EditorTextField("笔记标题");
    tfContent = new EditorTextField();
    tfContent.setPlaceholder("请输入笔记内容"); // 提示信息
    tfContent.setPreferredSize(new Dimension(200,100)); // 输入框大小
    panel.add(tfTitle,BorderLayout.NORTH);// 将空间位于北方(上方)
    panel.add(tfContent,BorderLayout.CENTER);// 位于中间
    return panel;
}
上次更新: 2 年前