从jtabbed窗格中的组件jlabel获取标题



我需要从select选项卡Jlabel(代码中的tabLabel)中获取文本,以便在程序中使用它

public void add() {
    final JPanel content = new JPanel();
    content.add(new JButton("kash"));
    JPanel tab = new JPanel();
    tab.setOpaque(false);
    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int closeTabNumber = tabbedPane.indexOfComponent(content);
            tabbedPane.removeTabAt(closeTabNumber);
        }
    });
    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);
    tabbedPane.addTab("aaa", content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
}

mouseClicked(MouseEvent)事件添加到tabLabel,并将此函数tabLabel.getText()写入事件函数中。

查看如何添加鼠标事件

最新更新