官方文档
https://www.jetbrains.org/intellij/sdk/docs/user_interface_components/misc_swing_components.html
Github
https://github.com/kungyutucheng/my_gradle_plugin
运行环境
macOS 10.14.5
IntelliJ idea 2019.2.4
效果
点击第一个tab
点击第二个tab
Demo
JBTabAction
package com.kungyu.jbtab;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;
/**
* @author wengyongcheng
* @since 2020/3/8 3:41 下午
*/
public class JBTabAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
new JBTabDialogWrapper(e.getProject()).showAndGet();
}
}
注册Action
<action id="com.kungyu.jbtab.JBTabAction" class="com.kungyu.jbtab.JBTabAction"
text="JBTabAction" description="JBTabAction">
<add-to-group group-id="ToolsMenu" anchor="after" relative-to-action="com.kungyu.jbsplitter.JBSplitterAction"/>
</action>
JBTabDialogWrapper
package com.kungyu.jbtab;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.tabs.JBTabs;
import com.intellij.ui.tabs.TabInfo;
import com.intellij.ui.tabs.impl.JBTabsImpl;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
/**
* @author wengyongcheng
* @since 2020/3/8 3:41 下午
*/
public class JBTabDialogWrapper extends DialogWrapper {
private JBTabs tabs;
private Project project;
public JBTabDialogWrapper(Project project) {
super(true);
this.project = project;
init();
}
@Nullable
@Override
protected JComponent createCenterPanel() {
tabs = new JBTabsImpl(project);
JPanel firstTabPanel = new JPanel();
firstTabPanel.add(new JLabel("First Tab"));
TabInfo firstTabInfo = new TabInfo(firstTabPanel);
firstTabInfo.setText("First Tab");
tabs.addTab(firstTabInfo);
JPanel secondTabPanel = new JPanel();
secondTabPanel.add(new JLabel("Second Tab"));
TabInfo secondTabInfo = new TabInfo(secondTabPanel);
secondTabInfo.setText("Second Tab");
tabs.addTab(secondTabInfo);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(tabs.getComponent(), BorderLayout.CENTER);
return panel;
}
}
注意,构造函数中,project的赋值必须早于init方法,因为init方法内部调用了createCenterPanel方法,而JBTabsImpl构造函数中的project参数又是必填的










网友评论