2016-11-18 35 views
0

我想写一个Scala接口,点击图像绘制彩色列。我还想检测是否按下了Enter键。这里是我暂时代码:Scala Swing GUI不响应按下的键

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不起作用。你能帮我解决这个问题吗?

+0

在JButton中使用图像作为'ImageIcon'。向该按钮添加一个“ActionListener”。然后,它应该对鼠标点击和按键进行响应。 –

回答

0

如果标签封装在一个面板中,它可以工作。标签将鼠标和面板监听到键盘。