读书人

JTabbedPane上的关闭图标如何实现

发布时间: 2012-07-05 07:59:18 作者: rapoo

JTabbedPane上的关闭图标怎么实现
JTabbedPane上的关闭图标怎么实现

[解决办法]
package xzfx.haveAtry;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
*
* @author guy
* @company yesoft
* @date 2009-1-16
*/
public class TabbedPanelShutTry implements ActionListener{


private JFrame frame;
private JTabbedPane tabbedPane;
private JButton addTabButton;
private ImageIcon closexIcon;
private Dimension closeButtonSize;
private int tabCounter = 0;

public TabbedPanelShutTry()
{
tabbedPane = new JTabbedPane();
addTabButton = new JButton("add tab");
addTabButton.addActionListener(this);

frame = new JFrame();
closexIcon = new ImageIcon("c:/shut.jpg");
closeButtonSize = new Dimension(closexIcon.getIconWidth()+2,closexIcon.getIconHeight()+2);
frame.add(tabbedPane,BorderLayout.CENTER);
frame.add(addTabButton,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setMinimumSize(new Dimension(300,300));
frame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {

final JPanel content = new JPanel();
JPanel tab = new JPanel();
tab.setOpaque(false);

JLabel tablabel = new JLabel("tab"+(++tabCounter));
JButton tabCloseButton = new JButton(closexIcon);


tabCloseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int closerTabNumber = tabbedPane.indexOfComponent(content);
System.out.println(closerTabNumber);
tabbedPane.removeTabAt(closerTabNumber);
}
});

tab.add(tablabel,BorderLayout.WEST);
tab.add(tabCloseButton,BorderLayout.EAST);

tabbedPane.addTab(null, content);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()-1, tab);
}

public static void main(String[] args)
{
TabbedPanelShutTry main = new TabbedPanelShutTry();
}
}

读书人网 >J2SE开发

热点推荐