【发布时间】:2016-11-18 14:01:46
【问题描述】:
我想编写一个 Scala 界面,在其中单击图像以绘制彩色列。我还想检测是否按下了 Enter 键。这是我的暂定代码:
def main = {
def top = new MainFrame {
title = "Reactive Swing App"
val label = new Label {
var matRaw, mat = new Mat
mat = Imgcodecs.imread("data/batch/14-ZoneDetection/01-noExtraCol/VISSAGE_115.jp2", Imgcodecs.CV_LOAD_IMAGE_COLOR) // images are stored as 8UC3
Image.printCaracColor("mat", mat)
var buf = toBufferedImage(mat)
icon = new ImageIcon(buf)
listenTo(mouse.clicks) // mouse.clicks is a member of Label, this is why the click position is relative to the label
listenTo(keys)
reactions += {
case MousePressed(_, point, _, _, _) => {
mat.submat(0, mat.rows, point.getX.toInt, point.getX.toInt + 1).setTo(new Scalar(0.0, 255.0, 0.0))
buf = toBufferedImage(mat)
icon = new ImageIcon(buf)
println("Click position, x: " + point.getX + ", y: " + point.getY)
}
}
reactions += {
case KeyPressed(_, Key.Enter, _, _) => {
println("Enter pressed")
}
}
}
contents = label
}
val frame = top
frame.resizable = false
frame.visible = true
}
case MousePressed 工作正常,绿色列正确添加到显示的图像中。但是,case KeyPressed 不起作用。你能帮我解决这个问题吗?
【问题讨论】:
-
在
JButton中使用ImageIcon的图像。将ActionListener添加到按钮。然后它应该响应鼠标点击和按键。
标签: swing scala user-interface reactive-programming