问题描述
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");
}
});
如果我点击带它的工作原理,但是当我点击按钮不起作用
It works if I click on the strip, but does not work when I click on the buttons
推荐答案
的按钮在JScrollBar中的UI定义,因此,你需要扩展默认的UI实现。当然,这是依赖于平台。在我的例子,我会告诉你如何使用 BasicScrollBarUI
来做到这一点。
你可以通过调用自定义一个JScrollBar中的 JScrollPane.setVerticalScrollBar(新CustomScrollBar());
在你CustomScrollBar你可以做到以下几点:
The buttons are defined in the JScrollBar's UI so you need to extend the default UI implementation. Of course it is platform dependent. In my example I'll show you how to do it with BasicScrollBarUI
.You can define a custom JScrollBar by calling the JScrollPane.setVerticalScrollBar(new CustomScrollBar());
In your CustomScrollBar you can do the following:
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的
进行了测试。
我希望它能帮助!
I've tested it under XP but without a JScrollPane
.I hope it helps!
这篇关于MouseListener的JScrollBar的箭头按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!