溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用idea插件怎么制作一個彈出框

發(fā)布時間:2020-12-16 14:11:40 來源:億速云 閱讀:1196 作者:Leah 欄目:開發(fā)技術

本篇文章給大家分享的是有關使用idea插件怎么制作一個彈出框,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一、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>

實際效果

使用idea插件怎么制作一個彈出框

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);
 }

效果如下

使用idea插件怎么制作一個彈出框

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); 會返回獲取焦點的組件
比如

使用idea插件怎么制作一個彈出框

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插件怎么制作一個彈出框,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI