idea插件開(kāi)發(fā)之彈出框的示例代碼
前言
IntelliJ平臺(tái)的用戶界面廣泛使用彈出窗口,即沒(méi)有chrome(顯式關(guān)閉按鈕)的半模式窗口,在焦點(diǎn)丟失時(shí)自動(dòng)消失。在插件中使用這些控件可以確保插件和IDE其他部分之間的用戶體驗(yàn)一致。
彈出窗口可以選擇性地顯示標(biāo)題,也可以移動(dòng)和調(diào)整大?。ú⒅С钟涀∷鼈兊拇笮。⑶铱梢郧短祝ó?dāng)選擇一個(gè)項(xiàng)目時(shí)顯示另一個(gè)彈出窗口)。
一、JBPopupFactory
JBPopupFactory 是idea 提供給用戶自定義窗口的接口,比較常見(jiàn)的方法如下
- createComponentPopupBuilder() 允許您在彈出窗口中顯示任何Swing組件。
- createPopupChooserBuilder() 創(chuàng)建一個(gè)多選/單選框
- createConfirmation() 創(chuàng)建一個(gè)確認(rèn)框
- createActionGroupPopup() 創(chuàng)建一個(gè)顯示方法組的窗口,選中會(huì)執(zhí)行方法。
創(chuàng)建彈出窗口后,需要通過(guò)調(diào)用show() 方法之一來(lái)顯示它。您可以通過(guò)調(diào)用showInBestPositionFor() 讓IntelliJ平臺(tái)根據(jù)上下文自動(dòng)選擇位置,或者通過(guò)showUnderneathOf() 和ShowInCenter() 等方法顯式指定位置。
show() 方法立即返回,不等待彈出窗口關(guān)閉。
如果需要在彈出窗口關(guān)閉時(shí)執(zhí)行某些操作,可以使用addListener() 方法將偵聽(tīng)器附加到它,然后重寫(xiě)彈出試的方法,例如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); 會(huì)返回獲取焦點(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插件開(kāi)發(fā)之彈出框的示例代碼的文章就介紹到這了,更多相關(guān)idea彈出框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea創(chuàng)建的idea項(xiàng)目時(shí)springframework出現(xiàn)紅色的原因和解決方法
當(dāng)使用 IntelliJ IDEA 創(chuàng)建 Spring Framework 項(xiàng)目時(shí),springframework 出現(xiàn)紅色可能是因?yàn)橄嚓P(guān)的 Spring Framework 依賴沒(méi)有正確加載或項(xiàng)目的配置有問(wèn)題,本文給大家介紹了一些常見(jiàn)的原因和解決方法,需要的朋友可以參考下2023-09-09
Java集合Map的clear與new Map區(qū)別詳解
這篇文章主要介紹了Java集合Map的clear與new Map區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼
這篇文章主要介紹了java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼的相關(guān)資料,Java動(dòng)態(tài)加載類主要是為了不改變主程序代碼,通過(guò)修改配置文件就可以操作不同的對(duì)象執(zhí)行不同的功能,需要的朋友可以參考下2017-07-07
SpringBoot實(shí)現(xiàn)識(shí)別圖片中的身份證號(hào)與營(yíng)業(yè)執(zhí)照信息
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)識(shí)別圖片中的身份證號(hào)與營(yíng)業(yè)執(zhí)照信息,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01
Java中的String.valueOf()和toString()方法區(qū)別小結(jié)
字符串操作是開(kāi)發(fā)者日常編程任務(wù)中不可或缺的一部分,轉(zhuǎn)換為字符串是一種常見(jiàn)需求,其中最常見(jiàn)的就是String.valueOf()和toString()方法,本文主要介紹了Java中的String.valueOf()和toString()方法區(qū)別小結(jié),感興趣的可以了解一下2025-04-04
Mybatis Plus查詢時(shí)sql字段名大小寫(xiě)報(bào)錯(cuò)的解決
這篇文章主要介紹了Mybatis Plus查詢時(shí)sql字段名大小寫(xiě)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring Boot Web 靜態(tài)文件緩存處理的方法
本篇文章主要介紹了Spring Boot Web 靜態(tài)文件緩存處理的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
SpringBoot使用DevTools實(shí)現(xiàn)后端熱部署的過(guò)程詳解
在Spring Boot項(xiàng)目中,Spring Boot官方提供你了Devtools熱部署模塊,通過(guò)maven的方式導(dǎo)入就能使用,本文主要SpringBoot通過(guò)DevTools實(shí)現(xiàn)熱部署,感興趣的朋友一起看看吧2023-11-11

