java jtabbedpane 关闭,关闭单击的选项卡,而不是当前选择的选项卡JTabbedPane

I have this class inside my main class to put a close button on my jTabbedPane. The problem is that, for example I have opened three tabs: tab journal, contact, and upload , and the tab contact is the currently selected tab. When I try to close the journal tab which is NOT the selected tab, the one that closes is the currently selected tab.

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());

}

This is what I do to call the tab :

menuTabbedPane.setTabComponentAt(menuTabbedPane.indexOfComponent(jvPanel), new Tab("contactPanel"));

解决方案

Your actionPerformed() method calls your closeTab() method. Your closeTab() method removes the currently selected tab from the tabbed pane.

Instead, you need to remove the component that corresponds to your tab with the button that was clicked.

When you create your Tab, also pass into the constructor the component that is the tab pane content. Then, you can use that in your actionPerformed() method, and pass the component to closeTab()

public void actionPerformed(ActionEvent e)

{

closeTab(component);

}

private void closeTab(JComponent component)

{

menuTabbedPane.remove(component);

}

Here's a bit more context:

tab = new Tab("The Label", component); // component is the tab content

menuTabbedPane.insertTab(title, icon, component, tooltip, tabIndex);

menuTabbedPane.setTabComponentAt(tabIndex, tab);

And in Tab ...

public Tab(String label, final JComponent component)

{

...

closeTab.addActionListener(new ActionListner()

{

public void actionPerformed(ActionEvent e)

{

closeTab(component);

}

});

...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Java代码示例和对应的布局文件,用于创建和管理选项。 TabbedPaneExample.java代码: ```java import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; public class TabbedPaneExample extends JFrame { private JTabbedPane tabbedPane; public TabbedPaneExample() { setTitle("TabbedPane Example"); setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); // 创建一个选项面板 tabbedPane = new JTabbedPane(); // 添加第一个选项 JPanel panel1 = new JPanel(); panel1.add(new JLabel("This is Tab 1")); tabbedPane.addTab("Tab 1", panel1); // 添加第二个选项 JPanel panel2 = new JPanel(); panel2.add(new JLabel("This is Tab 2")); tabbedPane.addTab("Tab 2", panel2); // 添加第三个选项 JPanel panel3 = new JPanel(); panel3.add(new JLabel("This is Tab 3")); tabbedPane.addTab("Tab 3", panel3); // 将选项面板添加到窗口中 getContentPane().add(tabbedPane); pack(); setLocationRelativeTo(null); } public static void main(String[] args) { EventQueue.invokeLater(() -> { new TabbedPaneExample().setVisible(true); }); } } ``` tabbedpane_example_layout.xml布局文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <gridbaglayout> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jtabbedpane> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jpanel> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jlabel text="This is Tab 1"/> </gridbagconstraints> </jpanel> </gridbagconstraints> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jpanel> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jlabel text="This is Tab 2"/> </gridbagconstraints> </jpanel> </gridbagconstraints> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jpanel> <gridbagconstraints fill="both" gridx="0" gridy="0" weightx="1" weighty="1"> <jlabel text="This is Tab 3"/> </gridbagconstraints> </jpanel> </gridbagconstraints> </jtabbedpane> </gridbagconstraints> </gridbaglayout> ``` 在这个例子中,我们使用了GridBagLayout布局来创建窗口,并在布局文件中嵌入了JTabbedPane、JPanel和JLabel组件。最终,我们在Java代码中读取布局文件,并将选项面板添加到窗口中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值