Java 如何在现有选项卡之间插入新选项卡而不删除任何选项卡?

Java 如何在现有选项卡之间插入新选项卡而不删除任何选项卡?,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,我需要一个方法,可以在指定的索引插入新的标签页和标签页后,该索引应该去的权利 我不想删除新选项卡之后的所有选项卡并将它们重新插入。我只想在现有选项卡之间添加新选项卡 守则: public class MainTabbedPane extends JTabbedPane { private SyntaxHighlighterManager syntaxHighlighterManager; private Map<Integer, Rectangle> tabsBou

我需要一个方法,可以在指定的索引插入新的标签页和标签页后,该索引应该去的权利

我不想删除新选项卡之后的所有选项卡并将它们重新插入。我只想在现有选项卡之间添加新选项卡

守则:

public class MainTabbedPane extends JTabbedPane {

    private SyntaxHighlighterManager syntaxHighlighterManager;
    private Map<Integer, Rectangle> tabsBounds = new HashMap<>();

    public MainTabbedPane() {
        this.syntaxHighlighterManager = SyntaxHighlighterManager.getInstance();

        Map<String, Action> actions = MainFrame.getInstance().getActions();
        Action closeTabAction = actions.get(CloseTabAction.CLOSE_TAB);
        Action closeAllTabsAction = actions.get(CloseAllTabsAction.CLOSE_ALL_TABS);
        Action closeAllTabsButThis = actions.get(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);

        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeTabAction.getValue(Action.ACCELERATOR_KEY), CloseTabAction.CLOSE_TAB);
        super.getActionMap().put(CloseTabAction.CLOSE_TAB, closeTabAction);
        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeAllTabsAction.getValue(Action.ACCELERATOR_KEY), CloseAllTabsAction.CLOSE_ALL_TABS);
        super.getActionMap().put(CloseAllTabsAction.CLOSE_ALL_TABS, closeAllTabsAction);
        super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                (KeyStroke) closeAllTabsButThis.getValue(Action.ACCELERATOR_KEY),
                CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS);
        super.getActionMap().put(CloseAllTabsButThisAction.CLOSE_ALL_BUT_THIS, closeAllTabsButThis);

        // super.setUI(new MainTabUI());
        // TabReorderHandler.enableReordering(this);
    }

    /**
     * Central method for adding new tab.
     * 
     * @param title
     * @param fileViewer
     * @param tip
     */
    private void addNewTab(String title, Container fileViewer, String tip) {
        if (fileViewer != null) {
            super.addTab(title, null, fileViewer, tip);
            // icon is set in tabComponent MainTabComponent
            super.setTabComponentAt(super.getTabCount() - 1, new MainTabComponent(title, this));
            tabsBounds.put(super.getTabCount() - 1, super.getUI().getTabBounds(this, super.getTabCount() - 1));
        }
    }
public类MainTabbedPane扩展了jtabedpane{
私有SyntaxHighlighterManager SyntaxHighlighterManager;
私有映射tabsBounds=newhashmap();
公共主选项卡窗格(){
this.syntaxHighlighterManager=syntaxHighlighterManager.getInstance();
映射操作=MainFrame.getInstance().getActions();
Action closeTabAction=actions.get(closeTabAction.CLOSE_选项卡);
Action closeAllTabsAction=actions.get(closeAllTabsAction.CLOSE\u所有选项卡);
Action closeAllTabsButThis=actions.get(CloseAllTabsButThisAction.closeu除此之外的所有对象);
super.getInputMap(JComponent.WHEN在聚焦窗口中)。放置(
(击键)closeTabAction.getValue(Action.ACCELERATOR\u键),closeTabAction.CLOSE\u选项卡);
super.getActionMap().put(CloseTabAction.CLOSE_选项卡,CloseTabAction);
super.getInputMap(JComponent.WHEN在聚焦窗口中)。放置(
(击键)closeAllTabsAction.getValue(Action.ACCELERATOR\u键),closeAllTabsAction.CLOSE\u所有选项卡);
super.getActionMap().put(CloseAllTabsAction.CLOSE_所有选项卡,CloseAllTabsAction);
super.getInputMap(JComponent.WHEN在聚焦窗口中)。放置(
(击键)关闭所有选项卡按钮此.getValue(Action.ACCELERATOR_键),
关闭所有选项卡按钮此操作。关闭除此之外的所有选项卡);
super.getActionMap().put(CloseAllTabsButThisAction.CLOSE_ALL_但_THIS,closeAllTabsButThis);
//super.setUI(新的MainTabUI());
//TabReorderHandler.enableReordering(此);
}
/**
*用于添加新选项卡的中心方法。
* 
*@param title
*@param文件查看器
*@param-tip
*/
私有void addNewTab(字符串标题、容器文件查看器、字符串提示){
if(fileViewer!=null){
super.addTab(title,null,fileViewer,tip);
//图标在选项卡组件Main选项卡组件中设置
super.setTabComponentAt(super.getTabCount()-1,新的MainTabComponent(title,this));
tabsBounds.put(super.getTabCount()-1,super.getUI().gettabounds(this,super.getTabCount()-1));
}
}
方法
addNewTab
添加新选项卡

谢谢!

JTabbedPane中的方法:

public void insertTab(String title,
         Icon icon,
         Component component,
         String tip,
         int index)
在给定索引处为给定组件插入一个新选项卡,由给定标题和/或图标表示,其中任何一个都可能为空

参数:

title - the title to be displayed on the tab
icon - the icon to be displayed on the tab
component - the component to be displayed when this tab is clicked.
tip - the tooltip to be displayed for this tab
index - the position to insert this new tab (> 0 and <= getTabCount())
title-要在选项卡上显示的标题
图标-要在选项卡上显示的图标
组件-单击此选项卡时要显示的组件。
提示-要为此选项卡显示的工具提示
索引-插入此新选项卡的位置(>0和getTabCount())
资料来源:

显示创建tabedpanetabbedPane.setTabComponentAt(0,新JLabel(“新选项卡”))的代码;我需要添加组件和选项卡组件。发布什么代码。问题很简单-我需要一种方法,可以在现有选项卡的位置插入新选项卡而不删除它。谢谢,但这种方法会删除
索引处的选项卡。然后才插入新选项卡。我不想丢失
索引处的上一个选项卡,你确定吗??因为我刚刚运行了一个示例代码,它在给定索引处创建并添加了一个新选项卡,移动选项卡(不替换任何选项卡)。我的选项卡窗格有3个选项卡,我执行了这行
jTabbedPane1.insertTab(“tab new”,null,new JPanel(),null,0);
测试各种索引值。它没有替换任何选项卡,但每次都添加了新选项卡。
IndexOutOfBoundsException - if the index is out of range (< 0 or > getTabCount())