【问题标题】:Scala Swing GUI does not respond to pressed keyScala Swing GUI 不响应按键
【发布时间】: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


【解决方案1】:

如果标签被封装在面板中,它就可以工作。标签听鼠标,面板听键盘。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多