美文网首页idea插件开发
2019-05-06 idea 插件开发——自定义界面

2019-05-06 idea 插件开发——自定义界面

作者: 袁成_0e4c | 来源:发表于2019-05-06 17:06 被阅读0次

准备:

1.idea 插件开发基础篇:https://www.jianshu.com/p/62d39ad54772

2.idea 插件开发官方文档:http://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/coordinates_system.html

开始:

1.新建dialog


新建dialog 新建dialog 生成的两个文件

2.编写界面

打开xxxDialog.form

右边有很多组件,拖进去就行了,各组件具体的api请自行探索。

页面的变动会自动同步到 xxxDialog.java中,命名由左边蓝色选中的 "field name"属性决定。

做个简单的功能吧——在光标位置插入一个空的method代码块

编辑界面 自动生成的代码

3.给按钮设置点击事件。。。看代码吧

import javax.swing.*;
import java.awt.event.*;

public class TestDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JButton intButton;
    private JButton shortButton;
    private JButton longButton;
    private JButton objectButton;

    public TestDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        ActionListener l = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (listener != null) {
                    listener.actionPerformed(e);
                }
            }
        };
        intButton.addActionListener(l);
        shortButton.addActionListener(l);
        longButton.addActionListener(l);
        objectButton.addActionListener(l);
    }

    private ActionListener listener;

    public void setListener(ActionListener listener) {
        this.listener = listener;
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }
}

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SampleAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Project project = anActionEvent.getProject();

        TestDialog dialog = new TestDialog();
        dialog.setListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = "public " + e.getActionCommand() + " methodName() {return null;}";
                WriteCommandAction.runWriteCommandAction(project, () -> {
                    Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
                    CaretModel caretModel = editor.getCaretModel();
                    int offset = caretModel.getOffset();
                    editor.getDocument().insertString(offset, str);
                });
            }
        });
        dialog.setLocationRelativeTo(dialog);//居中
        dialog.pack();
        dialog.setVisible(true);

    }
}

4.运行


打开一个java文件,alt + k 点击Object按钮,在光标位置加入了一段代码
5.打包
打出xxx.jar
jar的位置
6.安装:打开idea的设置-plugin,将刚准备好的jar安装上。
7.其他:快下班了,不写了。
8.分享一个自己用的插件
批量选择文件,copy到指定的目录
下载链接:https://github.com/cryzahell/ideapluginimageplus/raw/master/ideapluginimageplus.jar
github项目链接:https://github.com/cryzahell/ideapluginimageplus

相关文章

  • 2019-05-06 idea 插件开发——自定义界面

    准备: 1.idea 插件开发基础篇:https://www.jianshu.com/p/62d39ad54772...

  • 笔记:idea插件开发①

    这次idea插件开发需求中主要是对动作、service、数据存储以及界面几方面的处理,这里做一些idea插件开发记...

  • idea2019.3.4注册

    请购买正版 下载插件:提取码:b91n 打开idea,把插件拖入开发界面即可,有效期2089年

  • intellj 插件开发

    IntelliJ IDEA 插件开发详细视频教程 IntelliJ IDEA插件开发实战开发工具下载https:/...

  • IDEA设置

    IDEA设置中文界面 idea需要安装插件来该换为中文界面。文件 -> 设置 -> 插件,搜索框输入关键词 chi...

  • androidStudi自定义插件

    编写一个自定义Android Studio翻译插件 开发环境IDEA, 1.CreateProject- 2.Ne...

  • IDEA各种好用的插件

    IDEA 插件安装 步骤 重启 IDEA 即可 !!! 实用的IDEA 插件集合 Lombok plugin开发神...

  • EasyCode生成Java代码

    简介 EasyCode是基于IntelliJ IDEA Ultimate版开发的一个代码生成插件,主要通过自定义模...

  • 效率工具

    IDEA效率插件 EasyCode:基于 IntelliJ IDEA 开发的代码生成插件 EasyCode lom...

  • IDEA 插件开发-下载原生进度条

    分享几种IDEA 插件开发时原生下载方式和进度条自定义方法。 下载文件到本地 进度条自定义

网友评论

    本文标题:2019-05-06 idea 插件开发——自定义界面

    本文链接:https://www.haomeiwen.com/subject/xredoqtx.html