2

我想设置最常见的JTabbedPane鼠标事件行为,但找不到合适的选项来设置:

  1. 鼠标左键单击 - Select tab

  2. 鼠标右键单击 - Open current tab' dropdown menu

  3. 鼠标滚轮按钮单击 - Close the tab

问题:有没有办法实现它们?

PS:这里的任何例子都可以是SSCCE.

4

1 回答 1

7

默认情况下使用鼠标左键执行选项卡选择,因此您不需要添加该功能。您可以在这个小示例中找到的所有其他内容:

public static void main ( String[] args )
{
    final JFrame frame = new JFrame ();

    final JTabbedPane tabbedPane = new JTabbedPane ();
    tabbedPane.addTab ( "tab1", new JLabel ( "" ) );
    tabbedPane.addTab ( "tab2", new JLabel ( "" ) );
    tabbedPane.addTab ( "tab3", new JLabel ( "" ) );
    tabbedPane.addTab ( "tab4", new JLabel ( "" ) );
    frame.add ( tabbedPane );

    tabbedPane.setUI ( new MetalTabbedPaneUI ()
    {
        protected MouseListener createMouseListener ()
        {
            return new CustomAdapter ( tabbedPane );
        }
    } );

    frame.pack ();
    frame.setLocationRelativeTo ( null );
    frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
    frame.setVisible ( true );
}

private static class CustomAdapter extends MouseAdapter
{
    private JTabbedPane tabbedPane;

    public CustomAdapter ( JTabbedPane tabbedPane )
    {
        super ();
        this.tabbedPane = tabbedPane;
    }

    public void mousePressed ( MouseEvent e )
    {
        final int index = tabbedPane.getUI ().tabForCoordinate ( tabbedPane, e.getX (), e.getY () );
        if ( index != -1 )
        {
            if ( SwingUtilities.isLeftMouseButton ( e ) )
            {
                if ( tabbedPane.getSelectedIndex () != index )
                {
                    tabbedPane.setSelectedIndex ( index );
                }
                else if ( tabbedPane.isRequestFocusEnabled () )
                {
                    tabbedPane.requestFocusInWindow ();
                }
            }
            else if ( SwingUtilities.isMiddleMouseButton ( e ) )
            {
                tabbedPane.removeTabAt ( index );
            }
            else if ( SwingUtilities.isRightMouseButton ( e ) )
            {
                final JPopupMenu popupMenu = new JPopupMenu ();

                final JMenuItem addNew = new JMenuItem ( "Add new" );
                addNew.addActionListener ( new ActionListener ()
                {
                    public void actionPerformed ( ActionEvent e )
                    {
                        tabbedPane.addTab ( "tab", new JLabel ( "" ) );
                    }
                } );
                popupMenu.add ( addNew );

                final JMenuItem close = new JMenuItem ( "Close" );
                close.addActionListener ( new ActionListener ()
                {
                    public void actionPerformed ( ActionEvent e )
                    {
                        tabbedPane.removeTabAt ( index );
                    }
                } );
                popupMenu.add ( close );

                final JMenuItem closeAll = new JMenuItem ( "Close all" );
                closeAll.addActionListener ( new ActionListener ()
                {
                    public void actionPerformed ( ActionEvent e )
                    {
                        tabbedPane.removeAll ();
                    }
                } );
                popupMenu.add ( closeAll );

                final Rectangle tabBounds = tabbedPane.getBoundsAt ( index );
                popupMenu.show ( tabbedPane, tabBounds.x, tabBounds.y + tabBounds.height );
            }
        }
    }
}

当然,您最好将显示的菜单保存在某个地方,这样每次用户打开它时都不会重新创建它。您还可以将鼠标侦听器移动到单独的类中,以便在每次需要菜单和其他功能时使用它。

但我的目标是向您展示如何完成这些事情,而不是做一个完美的例子,所以我想开始使用选项卡式窗格已经绰绰有余了 :)

于 2013-08-07T15:02:39.533 回答