从jtabbedpane中的组件jlabel获取标题

时间:2013-11-22 07:01:22

标签: java swing jlabel jtabbedpane

我需要从选择标签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);
}

1 个答案:

答案 0 :(得分:-2)

mouseClicked(MouseEvent)事件添加到tabLabel并在事件功能中编写此函数tabLabel.getText()

See how to add MouseEvent