简体   繁体   English

如何引用我在 JScrollPane 中滚动的距离?

[英]How do I refer to the distance I’ve scrolled in a JScrollPane?

I'm writing code for drawing in a JPanel as if it's a canvas, where I can create shapes by clicking inside the panel.我正在编写用于在 JPanel 中绘图的代码,就好像它是 canvas 一样,我可以通过在面板内部单击来创建形状。 When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn't scrolled.当我单击时,它将点放置在我想要它们到 go 的位置,但是如果我滚动然后单击,它会将点放置在我没有滚动鼠标的位置。 I assume this is because the mouse coordinates don't change, so I'm guessing I need to add the number of pixels I've scrolled (horizontally and vertically).我认为这是因为鼠标坐标没有改变,所以我猜我需要添加我滚动的像素数(水平和垂直)。 Is there a command I can use to refer to these values?有没有可以用来引用这些值的命令?

It's been a while since I've worked with Swing, but I believe you want to call:自从我与 Swing 合作以来已经有一段时间了,但我相信您想致电:

JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();

The viewPosition.x and .y will return the offset in pixels from the top left corner. viewPosition.x.y将返回距左上角的像素偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM