0

我在 JTabbedPane 中动态添加和删除选项卡。代码:

     //Method which adds new tab  
     private void addTab(String title, MainPanel panel) {
        tabbed.addTab(title, panel);
        int index = tabbed.indexOfComponent(panel);
        JPanel tabPanel = new JPanel();
        tabPanel.setOpaque(false);
        tabPanel.add(panel.getLabel());
        JButton closeButton = new JButton(new CloseTabAc(index));
        tabPanel.add(closeButton);
        panel.getLabel().setText(title);
        tabbed.setTabComponentAt(index, tabPanel);
        tabbed.setSelectedIndex(index);
        currentPanel = panel;
    }

//Action to close tab
private class CloseTabAc extends AbstractAction {

    private int tabNum;

    CloseTabAc(int tabNum) {
        putValue(Action.NAME, "x");
        this.tabNum = tabNum;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainPanel panel = (MainPanel) tabbed.getComponentAt(tabNum);
                tabbed.remove(tabNum);
            }
        });
    }
}

所以这是代码。切换时,删除选项卡我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at javax.swing.JTabbedPane.getComponentAt(JTabbedPane.java:1224)
    at jnotepad.MainFrame$CloseTabAc$1.run(MainFrame.java:247)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    ...

我试图解决它几天,但我看不出有什么问题。我唯一看到此错误发生在我打开几个选项卡并随机关闭它们的情况下。所以例如。我打开 3 个选项卡:1、2 和 3。如果我按顺序关闭它们:3、2、1 我得到异常但一切都关闭了。但是当我尝试按顺序关闭它们时:3、1、2 然后 3 将关闭但在 1 上将引发异常并且它不会时不时地关闭。所以请帮我解决我的问题。

编辑:我根据让的解决方案修改了我的代码,现在我得到以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:371)
    at java.util.ArrayList.get(ArrayList.java:384)
    at javax.swing.JTabbedPane.getComponentAt(JTabbedPane.java:1224)
    at jnotepad.MainFrame$ChangeHandler.stateChanged(MainFrame.java:228)
    at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
    at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
    ...

看起来异常在第 228 行。这是我的 stateChanged 侦听器:

   private class ChangeHandler implements ChangeListener {
        @Override
        public void stateChanged(ChangeEvent ev) {
            currentPanel = (MainPanel) tabbed.getComponentAt(tabbed.getSelectedIndex());
        }
    }

每个选项卡中都有 MainPanel 实例。When another tab is selected I get this panel and set is as currentPanel in MainFrame. 这个异常不会像以前那样破坏我的 GUI,但我想摆脱它。

4

1 回答 1

1

如果你有三个标签

| 标签 0 | 标签 1 | 标签 2 |

如果您删除选项卡 1,选项卡 2 索引将为 1。这就是您获得IndexOutOfBoundsException.

将选项卡项的引用而不是其在选项卡式窗格中的索引传递给侦听器。

于 2013-03-09T15:16:51.920 回答