我的主类中有这个类,可以在我的 jTabbedPane 上放置一个关闭按钮。问题是,例如我打开了三个标签:标签日记、联系人和上传,标签联系人是当前选择的标签。当我尝试关闭不是选定选项卡的日记选项卡时,关闭的是当前选定的选项卡。
class Tab extends javax.swing.JPanel implements java.awt.event.ActionListener{
@SuppressWarnings("LeakingThisInConstructor")
public Tab(String label){
super(new java.awt.BorderLayout());
((java.awt.BorderLayout)this.getLayout()).setHgap(5);
add(new javax.swing.JLabel(label), java.awt.BorderLayout.WEST);
ImageIcon img = new ImageIcon(getClass().getResource("/timsoftware/images/close.png"));
javax.swing.JButton closeTab = new javax.swing.JButton(img);
closeTab.addActionListener(this);
closeTab.setMargin(new java.awt.Insets(0,0,0,0));
closeTab.setBorder(null);
closeTab.setBorderPainted(false);
add(closeTab, java.awt.BorderLayout.EAST);
}
@Override
public void actionPerformed(ActionEvent e) {
closeTab(); //function which closes the tab
}
}
private void closeTab(){
menuTabbedPane.remove(menuTabbedPane.getSelectedComponent());
}
这就是我调用选项卡的方法:
menuTabbedPane.setTabComponentAt(menuTabbedPane.indexOfComponent(jvPanel), new Tab("contactPanel"));