本文介绍了斯卡拉摇摆MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一些精心编写的示例项目,显示如何在Scala + Swing中实现MVP架构?



我在Scala + Swing中只发现了有关MVP的主题:








另外第二个额外的问题:你觉得,将GUI窗口小部件从视图展现给演示者是不对的使用public getter:

  def getNextButton():Reactor //此视图中的代码将由演示者访问设置clickHandler 

但是在traits中使用抽象字段(如第二个链接中所述 - ):

解决方案

不幸的是,Scala Swing放弃了相当多的底层Java Swing MVC。给出一些例子: ComboBox 没有直接访问模型(不同于$ code> JComboBox ), ListView Button 等只有有模型,但不是Scala 'fied,so notyped。



所以如果你想使用现有的Java模型,你需要进入对等体 Scala Swing小部件的字段。如果你想要MVC与你自己的模型,那么你必须手工接线。






  import scala.swing._ 
import Swing._

val m = new javax.swing.DefaultButtonModel
val cb = new CheckBox(检查){peer.setModel(m)}
val tb = new ToggleButton(Toggle){peer.setModel(m)}
val f = new Frame {
contents = new FlowPanel(cb,tb)
pack()。centerOnScreen()
open()
}

m addChangeListener ChangeListener {_ =>
println(sSelected?$ {m.isSelected})
}

m.setSelected(true)
pre>




这是一个在Scala中创建模型的小图书馆。


Does anybody know some well-written sample projects showing how to achieve MVP architecture in Scala+Swing?

I found only this topics about MVP in Scala + Swing:

And second extra question: how you think, it's right to expose GUI widgets from view to presenter not using public getters:

def getNextButton(): Reactor // this code in view will be accessed by presenter to setup clickHandler

but using abstract fields in traits (as described in second link I provide - Scala model-view-presenter, traits):

解决方案

Unfortunately, Scala Swing abandons quite a bit of the underlying Java Swing MVC. To give some examples: ComboBox has no direct access to the model (unlike JComboBox), neither does ListView, Button, etc. Only Table has the model, but not Scala'fied, so untyped.

So if you want to use the existing Java models, you need to go into the peer fields of the Scala Swing widgets. If you want MVC with your own models, well, then you'll have to do the wiring by hand.


import scala.swing._
import Swing._

val m  = new javax.swing.DefaultButtonModel
val cb = new CheckBox    ("Check" ) { peer.setModel(m) }
val tb = new ToggleButton("Toggle") { peer.setModel(m) }
val f  = new Frame {
  contents = new FlowPanel(cb, tb)
  pack().centerOnScreen()
  open()
}

m addChangeListener ChangeListener { _ =>
  println(s"Selected? ${m.isSelected}")
}

m.setSelected(true)


This is a tiny library to create models in Scala.

这篇关于斯卡拉摇摆MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:23