idea插件開發(fā)之彈出框的示例代碼
前言
IntelliJ平臺的用戶界面廣泛使用彈出窗口,即沒有chrome(顯式關閉按鈕)的半模式窗口,在焦點丟失時自動消失。在插件中使用這些控件可以確保插件和IDE其他部分之間的用戶體驗一致。
彈出窗口可以選擇性地顯示標題,也可以移動和調(diào)整大小(并支持記住它們的大?。?,并且可以嵌套(當選擇一個項目時顯示另一個彈出窗口)。
一、JBPopupFactory
JBPopupFactory 是idea 提供給用戶自定義窗口的接口,比較常見的方法如下
- createComponentPopupBuilder() 允許您在彈出窗口中顯示任何Swing組件。
- createPopupChooserBuilder() 創(chuàng)建一個多選/單選框
- createConfirmation() 創(chuàng)建一個確認框
- createActionGroupPopup() 創(chuàng)建一個顯示方法組的窗口,選中會執(zhí)行方法。
創(chuàng)建彈出窗口后,需要通過調(diào)用show() 方法之一來顯示它。您可以通過調(diào)用showInBestPositionFor() 讓IntelliJ平臺根據(jù)上下文自動選擇位置,或者通過showUnderneathOf() 和ShowInCenter() 等方法顯式指定位置。
show() 方法立即返回,不等待彈出窗口關閉。
如果需要在彈出窗口關閉時執(zhí)行某些操作,可以使用addListener() 方法將偵聽器附加到它,然后重寫彈出試的方法,例如onChosen(),或在彈出窗口中將事件處理程序附加到您自己的組件。
二、demo
1.showInBestPositionFor
Shows the popup in the position most appropriate for the specified data context.
在最適合指定數(shù)據(jù)上下文的位置顯示彈出窗口。
acaction 定義按鈕功能
public class TextBoxes extends AnAction { public TextBoxes() { super("MYSQL_COLUMN_ADD_PRO"); } @Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務 Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); popup.showInBestPositionFor(event.getDataContext()); } }
plugins.xml
<idea-plugin> <id>org.example.myPlugins</id> <name>MyPlugin</name> <vendor email="1585946147@qq.com" url="http://www.baidu.com">lieying</vendor> <description>first test plugin</description> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <actions> <!-- Add your actions here --> <group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu"> <add-to-group group-id="MainMenu" anchor="last" /> <action id="Myplugin.Textboxes" class="com.hunt.plugin.TextBoxes" text="Text _Boxes" description="A test menu item"> <keyboard-shortcut keymap="$default" first-keystroke="ctrl alt Z" /> </action> </group> </actions> </idea-plugin>
實際效果
2.show()
Shows the popup at the specified point.
顯示指定點的彈出窗口。
@Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務 Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); // 固定指定一個點顯示 Point point = new Point(200,300); RelativePoint relativePoint = new RelativePoint(point); popup.show(relativePoint); }
效果如下
3.showUnderneathOf()
Shows the popup at the bottom left corner of the specified component.
顯示指定組件左下角的彈出窗口。
@Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務 Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); // 獲取焦點的組件 Component component = event.getDataContext().getData(PlatformDataKeys.CONTEXT_COMPONENT); // 組件下方顯示 popup popup.showUnderneathOf(component); }
event.getDataContext().getData(PlatformDataKeys.CONTEXT_COMPONENT); 會返回獲取焦點的組件
比如
4.showInFocusCenter
Shows the popups in the center of currently focused component
在獲取焦點組件的中間彈出popup
@Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務 Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); popup.showInFocusCenter(); }
到此這篇關于idea插件開發(fā)之彈出框的示例代碼的文章就介紹到這了,更多相關idea彈出框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mybatis使用JSONObject接收數(shù)據(jù)庫查詢的方法
這篇文章主要介紹了Mybatis使用JSONObject接收數(shù)據(jù)庫查詢,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12SpringBoot實現(xiàn)阿里云快遞物流查詢的示例代碼
本文將基于springboot實現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2021-10-10一文帶你深入理解Java?AbstractQueuedSynchronizer
在并發(fā)編程中,鎖是一種保證線程安全的方式,這篇文章主要為大家介紹了AbstractQueuedSynchronizer(AQS)的數(shù)據(jù)結構及實現(xiàn)原理,感興趣的小伙伴可以了解一下2023-07-07