如何从选项卡窗格中删除选项卡

发布于 2024-12-27 10:31:33 字数 1999 浏览 1 评论 0原文

我创建了一个 Swing 应用程序,如下所示,当单击与特定任务相关的按钮时,它会在选项卡中显示主要任务。我在每个选项卡中添加了一个小的关闭按钮,我需要的是在单击与该选项卡相关的关闭按钮时关闭该选项卡。

在此处输入图像描述

关闭按钮位于从 JPanel 类扩展的类中,如下所示,

public class CloseTab extends JPanel {

    JLabel title = new JLabel();
    JButton closeButton = new JButton();
    int tabIndex;
    JTabbedPane tabbedPane = null;
    public static int SELECTED_TAB_INDEX;
    .
    .
    .    

    public static void setSELECTED_TAB_INDEX(int SELECTED_TAB_INDEX) {
        CloseTab.SELECTED_TAB_INDEX = SELECTED_TAB_INDEX;
    }

    .
    .

    public void setCloseAction(ActionListener al) {
        closeButton.addActionListener(al);
        closeButton.setSize(10, 10);
        closeButton.setBorder(new EmptyBorder(0, 0, 0, 0));
        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ofm/mnu/icons/delete.gif")));

    }

    public void setTabIndex(int index) {
        this.tabIndex = index;
        System.out.println(tabIndex);
    }

    public void init() {
        add(title);
        add(closeButton);
        setOpaque(false);
        setCloseAction(closeActoion);      

    }



    ActionListener closeActoion = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // System.out.println(tabIndex);
            if(tabbedPane.getTabCount() != 0 && tabbedPane.getSelectedIndex() == SELECTED_TAB_INDEX){
                tabbedPane.remove(SELECTED_TAB_INDEX);
            }   
        }
    };

}

在主框架中我按如下方式设置 SELECTED_TAB_INDEX 变量,

tbpWorkSpace.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                JTabbedPane a = (JTabbedPane) e.getSource();
                CloseTab pnl = new CloseTab();                
                pnl.setSELECTED_TAB_INDEX(a.getSelectedIndex());
            }
        });

但是,我无法获得我想要的结果,请告诉我是否有其他方法可以实现我想要的结果。

I have created a swing application as bellow which shows main tasks in tabs when clicking the buttons which are related to specific tasks. I have added a small close button to each tab and what I need to to is close the tab when clicking the close button related to that tab.

enter image description here

The close button is in a class which is extended fron JPanel class as bellow,

public class CloseTab extends JPanel {

    JLabel title = new JLabel();
    JButton closeButton = new JButton();
    int tabIndex;
    JTabbedPane tabbedPane = null;
    public static int SELECTED_TAB_INDEX;
    .
    .
    .    

    public static void setSELECTED_TAB_INDEX(int SELECTED_TAB_INDEX) {
        CloseTab.SELECTED_TAB_INDEX = SELECTED_TAB_INDEX;
    }

    .
    .

    public void setCloseAction(ActionListener al) {
        closeButton.addActionListener(al);
        closeButton.setSize(10, 10);
        closeButton.setBorder(new EmptyBorder(0, 0, 0, 0));
        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ofm/mnu/icons/delete.gif")));

    }

    public void setTabIndex(int index) {
        this.tabIndex = index;
        System.out.println(tabIndex);
    }

    public void init() {
        add(title);
        add(closeButton);
        setOpaque(false);
        setCloseAction(closeActoion);      

    }



    ActionListener closeActoion = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            // System.out.println(tabIndex);
            if(tabbedPane.getTabCount() != 0 && tabbedPane.getSelectedIndex() == SELECTED_TAB_INDEX){
                tabbedPane.remove(SELECTED_TAB_INDEX);
            }   
        }
    };

}

and in the main frame I seted the SELECTED_TAB_INDEX variable as follow,

tbpWorkSpace.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                JTabbedPane a = (JTabbedPane) e.getSource();
                CloseTab pnl = new CloseTab();                
                pnl.setSELECTED_TAB_INDEX(a.getSelectedIndex());
            }
        });

but, I couldn't get the result I wanted please tell me is there any other way to achieve the result I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2025-01-03 10:31:33

要删除选项卡,请使用 JTabbedPane 的 .remove(index) 方法。在此处了解更多信息:如何使用选项卡式窗格

To remove tab use .remove(index) method of JTabbedPane. Learn more here: How to Use Tabbed Panes

夏见 2025-01-03 10:31:33
private void removeTabWithTitle(String tabTitleToRemove) {
    for (int i = 0; i < tabbedPane.getTabCount(); i++) {
        String tabTitle = tabbedPane.getTitleAt(i);
        if (tabTitle.equals(tabTitleToRemove)) {
            tabbedPane.remove(i);
            break;
        }
    }
}
private void removeTabWithTitle(String tabTitleToRemove) {
    for (int i = 0; i < tabbedPane.getTabCount(); i++) {
        String tabTitle = tabbedPane.getTitleAt(i);
        if (tabTitle.equals(tabTitleToRemove)) {
            tabbedPane.remove(i);
            break;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文