如何将关闭按钮添加到JTabbedPane选项卡? [英] How to add close button to a JTabbedPane Tab?

查看:123
本文介绍了如何将关闭按钮添加到JTabbedPane选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JTabbedPane,我需要在选项卡中添加一个关闭按钮以关闭当前的关闭按钮。

I'm working in with a JTabbedPane, I need to add a close button in the tabs to close the current one.

我一直在寻找和我一样理解我必须从JPanel扩展并添加关闭按钮,因为他们说这里
但是,有没有办法添加延伸JTabbedPane的关闭按钮,或者有更简单的方法吗?

I have been searching and as I understand I must extend from JPanel and add the close button as they say here But, is there a way to add the close buttons extending JTabbedPane or is there a easier way to do it?

提前致谢,我非常感谢您的时间和帮助。

Thanks in advance, I really appreciate your time and your help.

推荐答案

基本上,您需要为选项卡提供渲染器。看看 JTabbedPane.setTabComponentAt(...)了解更多信息。

Essentially, you're going to need to supply a "renderer" for the tab. Take a look at JTabbedPane.setTabComponentAt(...) for more information.

基本思路是提供一个将在tab。

The basic idea is to supply a component that will be laid out on the tab.

我通常创建一个JPanel,我在其上添加一个JLabel(用于标题),并根据我想要显示的内容,采取某种行为控制作为关闭行动。

I typically create a JPanel, onto which I add a JLabel (for the title) and, depending on what I want to display, some kind of control that acts as the close action.

tabPane.addTab(title, tabBody);
int index = tabPane.indexOfTab(title);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
JLabel lblTitle = new JLabel(title);
JButton btnClose = new JButton("x");

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;

pnlTab.add(lblTitle, gbc);

gbc.gridx++;
gbc.weightx = 0;
pnlTab.add(btnClose, gbc);

tabPane.setTabComponentAt(index, pnlTab);

btnClose.addActionListener(myCloseActionHandler);

现在在其他地方,我建立了动作处理程序......

Now somewhere else, I establish the action handler...

public class MyCloseActionHandler implements ActionListener {

    public void actionPerformed(ActionEvent evt) {

        Component selected = tabPane.getSelectedComponent();
        if (selected != null) {

            tabPane.remove(selected);
            // It would probably be worthwhile getting the source
            // casting it back to a JButton and removing
            // the action handler reference ;)

        }

    }

}

现在,你刚才轻松使用您喜欢的任何组件并附加鼠标监听器并监控鼠标点击...

Now, you just as easily use any component you like and attach a mouse listener to it and monitor the mouse clicks...

更新

以上示例仅删除当前有效的标签,有几种方法可以解决此问题。

The above example will only remove the currently active tab, there are a couple of ways to fix this.

最好的方法是可能会提供一些方法来找到与其关联的标签...

The best is to probably provide some means for the action to find the tab it's associated with...

public class MyCloseActionHandler implements ActionListener {

    private String tabName;

    public MyCloseActionHandler(String tabName) {
        this.tabName = tabName;
    }

    public String getTabName() {
        return tabName;
    }

    public void actionPerformed(ActionEvent evt) {

        int index = tabPane.indexOfTab(getTabName());
        if (index >= 0) {

            tabPane.removeTabAt(index);
            // It would probably be worthwhile getting the source
            // casting it back to a JButton and removing
            // the action handler reference ;)

        }

    }

}   

这使用名称选项卡(与 JTabbedPane#addTab 一起使用)查找并删除选项卡及其相关组件...

This uses the name of tab (as used with JTabbedPane#addTab) to find and then remove the tab and its associated component...

这篇关于如何将关闭按钮添加到JTabbedPane选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆