使用鼠标滚动JTabbedPane中的选项卡 [英] Use mouse to scroll through tabs in JTabbedPane

查看:259
本文介绍了使用鼠标滚动JTabbedPane中的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动选项卡布局中有一个JTabbedPane,因此所有选项卡都很好地排成一行.有没有一种方法可以允许用户使用鼠标滚轮滚动浏览它们,还是通过键盘和GUI的箭头并单击选项卡来导航JTabbedPane.SCROLL_TAB_LAYOUT的唯一方法?

I have a JTabbedPane in a scroll tab layout, so that all the tabs sit nicely on one row. Is there a way to allow the user to scroll through them with the mouse wheel, or is the only way to navigate the JTabbedPane.SCROLL_TAB_LAYOUT with the keyboard's and GUI's arrows and by clicking the tabs?

推荐答案

在研究Eclipse的自动完成功能之后,我确实找到了答案:JTabbedPane使用MouseWheelListener:

I did find an answer to this, after digging around Eclipse's autocomplete: use a MouseWheelListener for the JTabbedPane:

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        JTabbedPane pane = (JTabbedPane) e.getSource();
        int units = e.getWheelRotation();
        int oldIndex = pane.getSelectedIndex();
        int newIndex = oldIndex + units;
        if (newIndex < 0)
            pane.setSelectedIndex(0);
        else if (newIndex >= pane.getTabCount())
            pane.setSelectedIndex(pane.getTabCount() - 1);
        else
            pane.setSelectedIndex(newIndex);
    }
});

这既允许鼠标在选项卡上滚动,又尊重JTabbedPane的索引范围.

This both allows for mouse scrolling over the tabs, and respects the index bounds of the JTabbedPane.

如果有人有更好的答案,我很乐意接受!

If anyone has a better answer, I'd be happy to accept!

这篇关于使用鼠标滚动JTabbedPane中的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆