Панель Java с вкладками: отобразить значок рядом с заголовком - PullRequest
5 голосов
/ 22 ноября 2010

можно ли отобразить значок "X" на заголовке вкладки, используемый для закрытия вкладки?Спасибо

Ответы [ 2 ]

6 голосов
/ 22 ноября 2010

Я бы предложил просмотреть учебник, Как использовать панели с вкладками и прокрутить вниз до раздела, озаглавленного Вкладки с пользовательскими компонентами .

Также, если вы посмотрите на ссылку пример индекса внутри этого раздела, приведен пример кода.

3 голосов
/ 22 ноября 2010

Я на самом деле только что создал реализацию этого сам. :)

Примерно так:

/* These need to be final so you can reference them in the MouseAdapter subclass
 * later. I personally just passed them to a method to add the tab, with the
 * parameters marked as final.
 * i.e., public void addCloseableTab(final JTabbedPane tabbedPane, ...)
 */
final Component someComponent = ...; //Whatever component is being added
final JTabbedPane tabbedPane = new JTabbedPane();
//I had my own subclass of AbstractButton, but that's irrelevant in this case
JButton closeButton = new JButton("x");

/*
 * titlePanel is initialized containing a JLabel with the tab title, 
 * and closeButton. (I don't recall the tabbed pane showing a title itself after 
 * setTabComponentAt() is called)
 */
JPanel titlePanel = ...;
tabbedPane.add(someComponent);
tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(someComponent), titlePanel);

closeButton.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        tabbedPane.remove(someComponent);
    }
 });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...