【问题标题】:Different scrolling speeds on a JComponent using a single JScrollBar使用单个 JScrollBar 在 JComponent 上的不同滚动速度
【发布时间】:2014-10-31 13:27:21
【问题描述】:

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

例如,假设我们有一个高度为 1000 的图像不适合我的框架。因此,我创建了一个默认的 JScrollBar,假设 viewport 的高度为 200:

[^]
(*) -> 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 中实现吗?

【问题讨论】:

  • JScrollBar 返回最小值和最大值,使用此值进行滚动增量

标签: java swing


【解决方案1】:

你可以重写 JScrollBar 方法

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

【讨论】:

  • 感谢您的回答。但这并不是我想要的。滚动条应该以相同的速度移动,但图像上的实际滚动速度应该根据旋钮位置增加/减少(就像我帖子上的图片一样)。因此,当比较两个滚动条(默认滚动条和我的“修改”)时,两个滚动条的旋钮位置完全相同,它们应该显示不同的视口。知道如何实现这一目标吗?也许这篇文章的标题有点误导。
  • 这是更复杂的事情。旋钮由 ScrollBarUI 渲染,它依赖于 L&F。看一下 BasicScrollBarUI 的 protected void setThumbBounds(int x, int y, int width, int height) 方法。你 cn 覆盖它以适应 Y
  • 感谢您的回答 StanislavL。我会检查一下。
猜你喜欢
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多