我一直在尝试在Scala中复制一些Java Swing工作。在下面的代码中this
扩展了Scala Swings ScrollPane
。
myPanel.reactions +={
case MouseDragged(_,x,_) => {
//val viewport = this.viewportView.get.asInstanceOf[JViewport]
val viewport = this.contents.head.self.asInstanceOf[JComponent].getParent.asInstanceOf[JViewport]
println(viewport.getViewPosition)
val vp = viewport.getViewPosition
var cp = x.getLocation
vp.translate(point.x-cp.x,point.y-cp.y)
//newLoc = new Point(vp.getX)
val jComp = contents.head.asInstanceOf[JComponent]
jComp.scrollRectToVisible(new Rectangle(vp,viewport.getSize: Dimension))
}
case MousePressed(_,x,_,_,_) => point = x.getLocation
case MouseClicked(_,_,_,_,_) => println("click")
}
我无法在相关问题的Java代码中看到如何访问Viewport
对象。如何在Scala Swing中实现它?
答案 0 :(得分:0)
myPanel.reactions +={
case MouseDragged(myPanel,x,_) => {
val cp = x.getLocation
this.horizontalScrollBar.value += point.x-cp.x
this.verticalScrollBar.value += point.y-cp.y
point = cp
}
case MousePressed(_,x,_,_,_) => point = x.getLocation
}