Scala Swing GUI不响应按下的键

时间:2016-11-18 14:01:46

标签: swing scala user-interface reactive-programming

我想编写一个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不起作用。你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果标签封装在面板中,则可以正常工作。标签会监听鼠标,面板会监听键盘。