使用单个JScrollBar在JComponent上进行不同的滚动速度

时间:2014-10-31 13:27:21

标签: java swing

我可以在JScrollBar上定义不同的滚动速度吗?

例如,假设我们绘制的高度为1000的图像不适合我的框架。因此,假设JScrollBar高度为200,我已按预期创建默认viewport

[^]
(*) -> The topmost pixel in the frame has height 0 of the image
 |
 |
 |
 |
[v]

[^]
 |
(*) -> The topmost pixel in the frame has height 200 of the image
 |
 |
 |
[v]

[^]
 |
 |
(*) -> The topmost pixel in the frame has height 400 of the image
 |
 |
[v]

现在我的问题:我可以改变这种行为吗?例如,我希望它以下列方式起作用:

[^]
(*) -> The topmost pixel in the frame has height 0 of the image!
 |
 |
 |
 |
[v]

[^]
 |
(*) -> The topmost pixel in the frame has height 300 of the image!
 |
 |
 |
[v]

[^]
 |
 |
(*)  -> The topmost pixel in the frame has height 650 of the image!
 |
 |
[v]

滚动条应该仍然与以前相同,但各个滚动速度会根据旋钮在滚动条上的位置而改变。理想情况下,我想定义一些滚动速度保持不变的区域。这个问题可能听起来很奇怪,但我需要这样一种机制,因为我正在动态地绘制JScrollBar的组件,而我事先并不知道它的确切大小。这种行为甚至可以在Swing中实现吗?

1 个答案:

答案 0 :(得分:3)

您可以覆盖JScrollBar方法

 public int getUnitIncrement(int direction) {
     int superValue=super.getUnitIncrement(direction);
//define a speedScaleValue is calculated based on value and maximum
     return superValue*speedScaleValue; 
 }