自定义带关闭按钮的TabbedPane

import javax.swing.JFrame;  
import javax.swing.JPanel;  
import javax.swing.JTabbedPane;  
import java.awt.Rectangle;  
import java.awt.Color;  
import java.awt.event.MouseListener;  
import java.awt.Component;  
import javax.swing.Icon;  
import java.awt.event.MouseEvent;  
import java.awt.Graphics;  
  
/** 
 *自定义带关闭按钮的TabbedPane 
 * @author  XiaoQuan 
 */  
public class ClosableTabbedPane extends JTabbedPane implements MouseListener {  
  
    private static final long serialVersionUID = -5154435928212725041L;  
  
    public ClosableTabbedPane() {  
        super();  
        addMouseListener(this);  
    }  
  
    @Override  
    public void addTab(String title, Component component) {  
        super.addTab(title, null, component);  
    }  
  
    public void addTab(String title, Component component, Icon extraIcon) {  
        super.addTab(title, new CloseTabIcon(), component);  
    }  
  
    @Override  
    public void mouseClicked(MouseEvent e) {  
        int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());  
        if (tabNumber < 0) {  
            return;  
        }  
        if (((CloseTabIcon) getIconAt(tabNumber)) != null) {  
            Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();  
            if (rect.contains(e.getX(), e.getY())) {  
                this.removeTabAt(tabNumber);  
            }  
        }  
    }  
  
    public void mouseEntered(MouseEvent e) {  
    }  
  
    public void mouseExited(MouseEvent e) {  
    }  
  
    public void mousePressed(MouseEvent e) {  
    }  
  
    public void mouseReleased(MouseEvent e) {  
    }  
      
    public static void main(String args[]){  
        JFrame f =new JFrame();  
          
        ClosableTabbedPane closableTabbedPane =new ClosableTabbedPane();  
        JPanel p2 = new JPanel();  
          
        closableTabbedPane.addTab("带关闭按钮", p2, null);  
          
        f.getContentPane().add(closableTabbedPane);  
        f.setSize(500,400);  
        f.setVisible(true);  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  
}  
  
class CloseTabIcon implements Icon {  
    private int x_pos;  
    private int y_pos;  
    private int width;  
    private int height;  
    
    public CloseTabIcon() {  
        width = 16;  
        height = 16;  
    }  
    @Override  
    public void paintIcon(Component c, Graphics g, int x, int y) {  
        this.x_pos = x;  
        this.y_pos = y;  
        Color col = g.getColor();  
        g.setColor(Color.RED.darker());  
        int y_p = y + 2;  
        g.drawLine(x + 1, y_p, x + 12, y_p);  
        g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);  
        g.drawLine(x, y_p + 1, x, y_p + 12);  
        g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);  
        g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);  
        g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);  
        g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);  
        g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);  
        g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);  
        g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);  
        g.setColor(col);  
    }  
  
    @Override  
    public int getIconWidth() {  
        return width;  
    }  
  
    @Override  
    public int getIconHeight() {  
        return height;  
    }  
  
    public Rectangle getBounds() {  
        return new Rectangle(x_pos, y_pos, width, height);  
    }  
} 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值