idea插件開發(fā)之彈出框的示例代碼
前言
IntelliJ平臺的用戶界面廣泛使用彈出窗口,即沒有chrome(顯式關(guān)閉按鈕)的半模式窗口,在焦點(diǎn)丟失時(shí)自動消失。在插件中使用這些控件可以確保插件和IDE其他部分之間的用戶體驗(yàn)一致。
彈出窗口可以選擇性地顯示標(biāo)題,也可以移動和調(diào)整大?。ú⒅С钟涀∷鼈兊拇笮。?,并且可以嵌套(當(dāng)選擇一個(gè)項(xiàng)目時(shí)顯示另一個(gè)彈出窗口)。
一、JBPopupFactory
JBPopupFactory 是idea 提供給用戶自定義窗口的接口,比較常見的方法如下
- createComponentPopupBuilder() 允許您在彈出窗口中顯示任何Swing組件。
- createPopupChooserBuilder() 創(chuàng)建一個(gè)多選/單選框
- createConfirmation() 創(chuàng)建一個(gè)確認(rèn)框
- createActionGroupPopup() 創(chuàng)建一個(gè)顯示方法組的窗口,選中會執(zhí)行方法。
創(chuàng)建彈出窗口后,需要通過調(diào)用show() 方法之一來顯示它。您可以通過調(diào)用showInBestPositionFor() 讓IntelliJ平臺根據(jù)上下文自動選擇位置,或者通過showUnderneathOf() 和ShowInCenter() 等方法顯式指定位置。
show() 方法立即返回,不等待彈出窗口關(guān)閉。
如果需要在彈出窗口關(guān)閉時(shí)執(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í)行的任務(wù) 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>
實(shí)際效果
2.show()
Shows the popup at the specified point.
顯示指定點(diǎn)的彈出窗口。
@Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務(wù) Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); // 固定指定一個(gè)點(diǎn)顯示 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í)行的任務(wù) Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); // 獲取焦點(diǎn)的組件 Component component = event.getDataContext().getData(PlatformDataKeys.CONTEXT_COMPONENT); // 組件下方顯示 popup popup.showUnderneathOf(component); }
event.getDataContext().getData(PlatformDataKeys.CONTEXT_COMPONENT); 會返回獲取焦點(diǎn)的組件
比如
4.showInFocusCenter
Shows the popups in the center of currently focused component
在獲取焦點(diǎn)組件的中間彈出popup
@Override public void actionPerformed(@NotNull AnActionEvent event) { // 獲取 JBPopupFactory JBPopupFactory instance = JBPopupFactory.getInstance(); // 創(chuàng)建需要執(zhí)行的任務(wù) Runnable runnable = new Runnable() { @Override public void run() { Messages.showMessageDialog("aaa", "hello", Messages.getInformationIcon()); } }; ListPopup popup = instance.createConfirmation("hello", runnable, 1); popup.showInFocusCenter(); }
到此這篇關(guān)于idea插件開發(fā)之彈出框的示例代碼的文章就介紹到這了,更多相關(guān)idea彈出框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis使用JSONObject接收數(shù)據(jù)庫查詢的方法
這篇文章主要介紹了Mybatis使用JSONObject接收數(shù)據(jù)庫查詢,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12java實(shí)現(xiàn)表格數(shù)據(jù)的存儲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)表格數(shù)據(jù)的存儲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04SpringBoot實(shí)現(xiàn)阿里云快遞物流查詢的示例代碼
本文將基于springboot實(shí)現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10一文帶你深入理解Java?AbstractQueuedSynchronizer
在并發(fā)編程中,鎖是一種保證線程安全的方式,這篇文章主要為大家介紹了AbstractQueuedSynchronizer(AQS)的數(shù)據(jù)結(jié)構(gòu)及實(shí)現(xiàn)原理,感興趣的小伙伴可以了解一下2023-07-07