关闭单击的选项卡,而不是当前选定的选项卡JTabbedPane(Close the clicked tab, not the currently selected tab JTabbedPane)

编程入门 行业动态 更新时间:2024-10-11 09:22:58
关闭单击的选项卡,而不是当前选定的选项卡JTabbedPane(Close the clicked tab, not the currently selected tab JTabbedPane)

我在我的主类中有这个类,在我的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"));

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

最满意答案

你的actionPerformed()方法调用你的closeTab()方法。 您的closeTab()方法从选项卡式窗格中删除当前选定的选项卡。

相反,您需要使用单击的按钮删除与选项卡对应的组件。

创建Tab ,还要将构造函数作为选项卡窗格内容传递给构造函数。 然后,您可以在actionPerformed()方法中使用它,并将组件传递给closeTab()

public void actionPerformed(ActionEvent e) { closeTab(component); } private void closeTab(JComponent component) { menuTabbedPane.remove(component); }

这里有更多的背景:

tab = new Tab("The Label", component); // component is the tab content menuTabbedPane.insertTab(title, icon, component, tooltip, tabIndex); menuTabbedPane.setTabComponentAt(tabIndex, tab);

并在Tab ...

public Tab(String label, final JComponent component) { ... closeTab.addActionListener(new ActionListner() { public void actionPerformed(ActionEvent e) { closeTab(component); } }); ... }

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); } }); ... }

更多推荐

本文发布于:2023-08-04 09:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   单击   而不是   JTabbedPane   selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!