JTabbedPane添加关闭按钮和缩略图 .

/*
* Arjick@163.com
*
*/
package exec;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.HashMap;
/**
* A JTabbedPane which has a close ('X') icon on each tab.
* To add a tab, use the method addTab(String, Component)
* To have an extra icon on each tab (e.g. like in JBuilder,
* showing the file type) use the method
* addTab(String, Component, Icon).
* Only clicking the 'X' closes the tab. */
public class JClosableTabbedPane extends JTabbedPane implements MouseListener {
    private double scaleRatio = 0.3;
    private HashMap<String, Component> maps = new HashMap<String, Component>();
    public JClosableTabbedPane() {
        super();
        addMouseListener(this);
    }
    public void addTab(String title, Component component) {
        this.addTab(title, component, null);
    }
    public void addTab(String title, Component component, Icon extraIcon) {
        super.addTab(title, new CloseTabIcon(extraIcon), component);
    }
    public void insertTab(String title, Icon icon, Component component, String tip, int index) {
        tip = "tab" + component.hashCode();
        maps.put(tip, component);
        super.insertTab(title, icon, component, tip, index);
    }
    public void removeTabAt(int index) {
        Component component = getComponentAt(index);
        maps.remove("tab" + component.hashCode());
        super.removeTabAt(index);
    }
    public JToolTip createToolTip() {
        ImageToolTip tooltip = new ImageToolTip();
        tooltip.setComponent(this);
        return tooltip;
    }
    class ImageToolTip extends JToolTip {
        public Dimension getPreferredSize() {
            String tip = getTipText();
            Component component = maps.get(tip);
            if (component != null) {
                return new Dimension((int) (getScaleRatio() * component.getWidth()), (int) (getScaleRatio() * component.getHeight()));
            } else {
                return super.getPreferredSize();
            }
        }
        public void paintComponent(Graphics g) {
            String tip = getTipText();
            Component component = maps.get(tip);
            if (component instanceof JComponent) {
                JComponent jcomponent = (JComponent) component;
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform at = g2d.getTransform();
                g2d.transform(AffineTransform.getScaleInstance(getScaleRatio(), getScaleRatio()));
                ArrayList<JComponent> dbcomponents = new ArrayList<JComponent>();
                updateDoubleBuffered(jcomponent, dbcomponents);
                jcomponent.paint(g);
                resetDoubleBuffered(dbcomponents);
                g2d.setTransform(at);
            }
        }
        private void updateDoubleBuffered(JComponent component, ArrayList<JComponent> dbcomponents) {
            if (component.isDoubleBuffered()) {
                dbcomponents.add(component);
                component.setDoubleBuffered(false);
            }
            for (int i = 0; i < component.getComponentCount(); i++) {
                Component c = component.getComponent(i);
                if (c instanceof JComponent) {
                    updateDoubleBuffered((JComponent) c, dbcomponents);
                }
            }
        }
        private void resetDoubleBuffered(ArrayList<JComponent> dbcomponents) {
            for (JComponent component : dbcomponents) {
                component.setDoubleBuffered(true);
            }
        }
    }
    public double getScaleRatio() {
        return scaleRatio;
    }
    public void setScaleRatio(double scaleRatio) {
        this.scaleRatio = scaleRatio;
    }
    public void mouseClicked(MouseEvent e) {
        int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
        if (tabNumber < 0) {
            return;
        }
        Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
        if (rect.contains(e.getX(), e.getY())) {
            //the tab is being closed
            this.removeTabAt(tabNumber);
        }
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
}
/**
* The class which generates the 'X' icon for the tabs. The constructor
* accepts an icon which is extra to the 'X' icon, so you can have tabs
* like in JBuilder. This value is null if no extra icon is required.
*/
class CloseTabIcon implements Icon {
    private int x_pos;
    private int y_pos;
    private int width;
    private int height;
    private Icon fileIcon;
    public CloseTabIcon(Icon fileIcon) {
        this.fileIcon = fileIcon;
        width = 16;
        height = 16;
    }
    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.black);
        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);
        if (fileIcon != null) {
            fileIcon.paintIcon(c, g, x + width, y_p);
        }
    }
    public int getIconWidth() {
        return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
    }
    public int getIconHeight() {
        return height;
    }
    public Rectangle getBounds() {
        return new Rectangle(x_pos, y_pos, width, height);
    }
    /*public static void main(String args[]) {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    JClosableTabbedPane pane = new JClosableTabbedPane();
    ImageIcon icon = new ImageIcon("images/middle.jpg");
    pane.addTab("tab1",new JButton("first Button"),icon);
    pane.addTab("tab2",new JButton("sec Button"),icon);
    pane.addTab("tab3",new JButton("third Button"),icon);
    pane.addTab("tab4",new JButton("fourth Button"),icon);
    JFrame frame = new JFrame("Demo");
    frame.getContentPane().add(pane,BorderLayout.CENTER);
    frame.setSize(500,300);
    frame.setLocation(300,200);
    frame.show();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }*/
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 MATLAB 的 Tab 标题栏上添加关闭按钮,需要使用 MATLAB 的 Java 组件。可以使用以下代码实现: ```matlab % 获取当前 Tab 组件对象 hTabGroup = uitabgroup('Parent', figure); hTab = uitab(hTabGroup, 'Title', 'Tab 1'); % 获取 Tab 标题栏的 Java 组件对象 jTabGroup = get(handle(hTabGroup), 'JavaFrame'); jTabbedPane = jTabGroup.getAxisComponent(jTabGroup.fFigureClient.getComponents); % 创建一个按钮 jCloseButton = javaObjectEDT('javax.swing.JButton', '关闭'); jCloseButton.setFocusable(false); jCloseButton.setBorder([]); jCloseButton.addActionListener(javaObjectEDT('java.awt.event.ActionListener', @closeTab)); % 将按钮添加到 Tab 标题栏 jTabbedPane.setTabComponentAt(jTabbedPane.indexOfComponent(hTab), jCloseButton); % 关闭 Tab 的回调函数 function closeTab(~, ~) jTabbedPane = get(gcbo, 'Parent'); tabIndex = jTabbedPane.getSelectedIndex() + 1; jTabbedPane.remove(tabIndex); end ``` 这里首先创建一个 Tab 组件对象 `hTabGroup` 和 Tab 对象 `hTab`。然后通过 `get(handle(hTabGroup), 'JavaFrame')` 获取 Tab 标题栏的 Java 组件对象 `jTabGroup`,并通过 `jTabGroup.getAxisComponent(jTabGroup.fFigureClient.getComponents)` 获取 Tab 组件的 TabbedPane 对象 `jTabbedPane`。 接着,使用 `javaObjectEDT` 函数创建一个按钮对象 `jCloseButton`,并为其添加一个关闭 Tab 的回调函数 `closeTab`。注意,这里的 `closeTab` 函数必须在 EDT 线程中执行,因此使用了 `javaObjectEDT` 函数进行封装。 最后,将按钮添加到 Tab 标题栏上,使用 `jTabbedPane.setTabComponentAt` 函数实现。当用户点击关闭按钮时,会调用 `closeTab` 函数,该函数会从 TabbedPane 中移除选中的 Tab。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值