如何将关闭按钮添加到 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.
基本思想是提供一个将在选项卡上布局的组件.
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...