如何将closuresbutton添加到JTabbedPane选项卡?

我正在使用JTabbedPane,我需要在选项卡中添加一个closuresbutton来closures当前的一个。

我一直在寻找,据我所知我必须从JPanel扩展,并添加closuresbutton,因为他们在这里说但是,有没有办法添加延伸JTabbedPane的closuresbutton,还是有一个更简单的方法来做到这一点?

在此先感谢,我真的很感激你的时间和你的帮助。

本质上,你将需要为该标签提供一个“渲染器”。 看看JTabbedPane.setTabComponentAt(…)了解更多信息。

基本的想法是提供一个将被放置在标签上的组件。

我通常创build一个JPanel,在其上添加一个JLabel(用于标题),并根据我要显示的内容,执行一些类似的控制操作。

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

现在在别的地方,我build立了行动处理程序…

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

现在,您可以轻松地使用任何您喜欢的组件,并将鼠标监听器附加到它并监视鼠标点击…

更新

上面的例子只会删除当前活动的选项卡,有几种方法可以解决这个问题。

最好的是可能提供一些手段,以find它与相关联的选项卡的行动…

 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 )来查找,然后删除选项卡及其关联的组件…

我发现一个标签的例子(从Java网站)似乎这样做,至less在他们的。 (虽然我以为,当我尝试过去,它也closures了当前选定的选项卡,虽然它运行正常,当你运行他们的例子,但我认为当我更新它在选项卡式的Java记事本上工作,它正在closures当前选中的标签,虽然也许我做错了。

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java

是的,我的东西正在工作! 这将工作的实际标签,而不是当前选定的标签!

希望你已经得到了你的问题的答案。 我想给一个对我非常有用的链接。

JTabbedPane与closuresbutton

以下是一些代码。

 public static void createAndShowGUI() { JFrame frame = new JFrame("Tabs"); frame.setMinimumSize(new Dimension(500, 200)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tabbedPane = new JTabbedPane(); JPanel panel = new JPanel(); panel.setOpaque(false); tabbedPane.add(panel); tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Tab1")); JPanel panel1 = new JPanel(); panel1.setOpaque(false); tabbedPane.add(panel1); tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel1), getTitlePanel(tabbedPane, panel1, "Tab2")); JPanel panel2 = new JPanel(); panel2.setOpaque(false); tabbedPane.add(panel2); tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel2), getTitlePanel(tabbedPane, panel2, "Tab3")); JPanel panel3 = new JPanel(); panel3.setOpaque(false); tabbedPane.add(panel3); tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel3), getTitlePanel(tabbedPane, panel3, "Tab4")); frame.add(tabbedPane); // Display the window. frame.pack(); frame.setVisible(true); } 

我在oracle的代码中做了一些改变。

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java

给图标添加标签,加上closures标签button。 希望有所帮助。

 public static void addTag(JTabbedPane tab, String title, Icon icon, int index){ MouseListener close = new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { //your code to remove component //I use this way , because I use other methods of control than normal: tab.remove(int index); } }; final ButtonClose buttonClose = new ButtonClose (title, icon, close ); tab.setTabComponentAt(index, buttonClose); tab.validate(); tab.setSelectedIndex(index); 

}

公共类ButtonCloseinheritanceJPanel {

 public ButtonClose(final String title, Icon icon, MouseListener e) { JLabel ic = new JLabel(icon); ic.setSize(icone.getIconWidth(), icone.getIconHeight()); JLabel text= new JLabel(title); text.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5)); ButtonTab button = new ButtonTab(); button.addMouseListener(e); button.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); JPanel p = new JPanel(); p.setSize(getWidth() - icone.getIconWidth(), 15); p.add(text); p.add(button); add(ic); add(p); } private class ButtonTab extends JButton { public ButtonTab() { int size = 13; setPreferredSize(new Dimension(size, size)); setToolTipText("Close"); setUI(new BasicButtonUI()); setFocusable(false); setBorderPainted(false); addMouseListener(listener); setRolloverEnabled(true); } @Override public void updateUI() { } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g.create(); if (getModel().isPressed()) { g2.translate(1, 1); } g2.setStroke(new BasicStroke(2)); g2.setColor(new Color(126, 118, 91)); if (getModel().isRollover()) { g2.setColor(Color.WHITE); } int delta = 3; g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1); g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1); g2.dispose(); } } private final MouseListener listener = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { Component component = e.getComponent(); if (component instanceof AbstractButton) { AbstractButton button = (AbstractButton) component; button.setContentAreaFilled(true); button.setBackground(new Color(215, 65, 35)); } } @Override public void mouseExited(MouseEvent e) { Component component = e.getComponent(); if (component instanceof AbstractButton) { AbstractButton button = (AbstractButton) component; button.setContentAreaFilled(false); //transparent } } }; 

}

在这里看看Peter-Swing。 它有一个JClosableTabbedPane类,以及其他许多其他类。

当你下载的jar文件,你可以运行它,并有所有类的例子。

您可以使用名为“x”的JLabel并使用mouseListener

  private final JLabel l = new JLabel(); // this is the label for tabbedPane private final JLabel b = new JLabel("x");//Close Button if (closeable) { b.setToolTipText("Click to close"); b.setOpaque(false); b.setBackground(Color.gray); b.addMouseListener(new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { b.setBorder(bordere); b.setOpaque(false); } @Override public void mouseEntered(MouseEvent e) { b.setBorder(borderl); } @Override public void mouseReleased(MouseEvent e) { b.setOpaque(false); b.repaint(); if (b.contains(e.getPoint())) { b.setBorder(borderl); if (confirmTabClosing()) { tab.remove(tabIndex()); if(tab.getTabCount() == 0) spacialTabComponent.maximizeOrRestore.doClick(); } } else b.setBorder(bordere); } @Override public void mousePressed(MouseEvent e) { b.setOpaque(true); b.repaint(); } }); b.setBorder(bordere); add(b, getLeftAlignedBothFilledGBC(1, 0, new Insets(0, 0, 0, 0), 0, 0)); } } 
 jbCloseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = jtbMainTabbedPane.indexOfTabComponent(jbCloseButton); jtbMainTabbedPane.remove(index); } });