1

我正在使用滚动窗格设置来使用自定义滚动条,除此之外,我还想在滚动条的拇指上安装一个侦听器,这样每当鼠标进入拇指区域时,它就可以改变颜色或边框。我搜索了 BasicScrollBarUI(由我的自定义滚动条 UI 扩展)并找到了 installListeners() 方法,因此我覆盖了它并让它为拇指区域再调用一个侦听器。

SSCCE:

public class TestScrollBar extends JFrame {

    public TestScrollBar() {

        JPanel innerPanel = new JPanel();
        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
        for (int i=1; i<=10; i++) {
            innerPanel.add(new JLabel("Label "+i));
            innerPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        }

        JScrollPane scrollPane = new JScrollPane(innerPanel);
        scrollPane.setPreferredSize(new Dimension(innerPanel.getPreferredSize().width, innerPanel.getPreferredSize().height/2));

        scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        this.setLayout(new BorderLayout());
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.NORTH);
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.SOUTH);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.EAST);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.WEST);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestScrollBar frame = new TestScrollBar();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

还有一个简单的滚动条自定义 UI:

public class CustomScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void installListeners() {
        super.installListeners();
        scrollbar.addMouseListener(new CustomListener());
    }

    protected class CustomListener extends MouseAdapter {
        public void mouseEntered(MouseEvent e) {
            if (getThumbBounds().contains(e.getX(), e.getY())) {
                System.out.println("THUMB");
            }
        }
    }
}

当鼠标从右侧或左侧进入拇指区域时,侦听器工作正常,但它并不总是适用于顶部或底部。对于这些侧面,它仅在拇指触摸其中一个按钮时才起作用。例如,当滚动条位于最顶部时,侦听器仅在鼠标从顶部按钮而不是从下方进入拇指区域时才起作用(左右侧除外)。当滚动条触摸减少按钮时,会发生相反的情况(仅适用于右侧/左侧/底部,但不适用于顶部)。当拇指介于两者之间而不接触任何按钮时,则只有左右两侧起作用。如果您运行代码,您可以看到所有这些。

我还尝试使用现有的侦听器之一(而不是创建自己的侦听器)BasicScrollBarUI.TrackListener,我在其中添加了一个仅侦听拇指区域的方法,但结果完全相同:

public class CustomScrollBarUI extends BasicScrollBarUI {
    @Override
    protected TrackListener createTrackListener(){
        return new TrackListener();
    }

    protected class TrackListener extends BasicScrollBarUI.TrackListener {
        public void mouseEntered(MouseEvent e) {
            currentMouseX = e.getX();
            currentMouseY = e.getY();
            if (getThumbBounds().contains(currentMouseX, currentMouseY)) {
                System.out.println("THUMB");
            }
        }
    }
}
4

3 回答 3

1

问题是您监听滚动条,这意味着它是拇指和拇指可以移动的区域(轨道)。因此,您在 mouseEntered 方法中的测试条件并不总是正确的。例如,假设拇指在垂直滚动条的顶部,从底部进入意味着您将首先进入滚动条轨道,然后将鼠标移动到拇指区域-->没有鼠标进入事件。

这是您的代码稍作修改的版本。它主要做的也是侦听鼠标“移动”事件(通过将侦听器添加为 MouseMotionListener)并且它按预期工作:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicScrollBarUI;

public class TestScrollBar extends JFrame {

    public static class CustomScrollBarUI extends BasicScrollBarUI {

        @Override
        protected void installListeners() {
            super.installListeners();
            CustomListener listener = new CustomListener();
            scrollbar.addMouseListener(listener);
            scrollbar.addMouseMotionListener(listener);
        }

        protected class CustomListener extends MouseAdapter {
            boolean isInsideThumb = false;

            @Override
            public void mouseEntered(MouseEvent e) {
                handleMouseEvent(e);
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                handleMouseEvent(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                handleMouseEvent(e);
            }

            private void handleMouseEvent(MouseEvent e) {
                if (getThumbBounds().contains(e.getX(), e.getY())) {
                    if (!isInsideThumb) {
                        System.out.println("THUMB");
                        isInsideThumb = true;
                    }
                } else {
                    if (isInsideThumb) {
                        System.out.println("OUT OF THUMB");
                        isInsideThumb = false;
                    }
                }
            }
        }
    }

    public TestScrollBar() {

        JPanel innerPanel = new JPanel();
        innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
        for (int i = 1; i <= 10; i++) {
            innerPanel.add(new JLabel("Label " + i));
            innerPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        }

        JScrollPane scrollPane = new JScrollPane(innerPanel);
        scrollPane.setPreferredSize(new Dimension(innerPanel.getPreferredSize().width, innerPanel.getPreferredSize().height / 2));

        scrollPane.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        this.setLayout(new BorderLayout());
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.NORTH);
        this.add(Box.createRigidArea(new Dimension(0, 20)), BorderLayout.SOUTH);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.EAST);
        this.add(Box.createRigidArea(new Dimension(20, 0)), BorderLayout.WEST);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestScrollBar frame = new TestScrollBar();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
于 2012-09-12T18:43:05.740 回答
1

可能无法回答您的问题,但是您已经查看了 AdjustmentListener,那么理论上在哪里转换 MousePoint 并不重要,这里有一些关于这个问题的例子

于 2012-09-12T20:31:15.953 回答
0

当您在轨道中而不是拇指中输入鼠标时,将激活 mouseEntered。您的代码仅在您离开赛道并同时进入赛道和拇指时才有效

改用 mouseMove 事件,你会得到它的工作:

@Override
public void mouseMoved(MouseEvent e) {
    currentMouseX = e.getX();
    currentMouseY = e.getY();
    if (getThumbBounds().contains(currentMouseX, currentMouseY)) {
        System.out.println("THUMB");
    }
}
于 2012-09-12T18:53:07.737 回答