如何从选项卡窗格中删除选项卡
我创建了一个 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要删除选项卡,请使用 JTabbedPane 的
.remove(index)
方法。在此处了解更多信息:如何使用选项卡式窗格To remove tab use
.remove(index)
method of JTabbedPane. Learn more here: How to Use Tabbed Panes