时不工作
http://s019.radikal.ru/i626/1203/ae/8420ef7757f7.png
JScrollPane.getVerticalScrollBar().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
});
如果我点击条形图,它可以工作,但当我点击按钮
按钮是在JScrollBar的UI中定义的,因此您需要扩展默认的UI实现。当然,它依赖于平台。在我的示例中,我将向您展示如何使用BasicScrollBarUI
。您可以通过调用JScrollPane.setVerticalScrollBar(new CustomScrollBar());
来定义自定义JScrollBar在您的自定义滚动条中,您可以执行以下操作:
public class CustomScrollBar extends JScrollBar {
public CustomScrollBar() {
setUI(new CustomUI());
}
class CustomUI extends BasicScrollBarUI {
@Override
protected void installListeners() {
super.installListeners();
if (incrButton != null) {
incrButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//Increment button is clicked!
}
});
}
if (decrButton != null) {
decrButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//Decrement button is clicked!
}
});
}
}
}
}
我在XP下测试过,但没有JScrollPane
。我希望它能有所帮助!