JTabbedPane的。选择鼠标左键单击选项卡

时间:2013-08-07 14:04:06

标签: java swing jtabbedpane

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

  1. 鼠标左键单击 - Select tab

  2. 正确鼠标左键单击 - Open current tab' dropdown menu

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

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

    PS: here中的任何示例都可以是SSCCE

1 个答案:

答案 0 :(得分:6)

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

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 );
            }
        }
    }
}

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

但我的目标是告诉你这些事情是如何完成的,而不是做出一个完美的例子,所以我认为开始使用标签式窗格已经足够了:)