我遇到了一些我认为是 scala 语法中的错误/无语法意义的东西,但我更喜欢在报告后将其发布在这里,因为我可能错了。
控制器类:
package some
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
class ItemController extends ActionListener {
val NEW_ITEM = "new item"
val UPDATE_ITEM = "update item"
private val newItem = new ItemNew
override def actionPerformed(e: ActionEvent) = {
}
def create(view: String) = {
view match {
case NEW_ITEM => {
newItem eraseForm //Here eclipse says "Unit does not take parameters".
newItem setVisible true
}
case UPDATE_ITEM =>
}
}
}
视图类
package some
import scala.collection.mutable.HashMap
import javax.swing.JPanel
import java.awt.GridLayout
import javax.swing.JFrame
import scala.collection.mutable.LinkedHashMap
import javax.swing.JTextField
import java.awt.BorderLayout
import java.awt.FlowLayout
import javax.swing.JLabel
class ItemNew extends JFrame {
private val formFields = LinkedHashMap[String, JTextField]()
init
private def init(): Unit = {
// defining form fields
formFields += ("name" -> new JTextField(20),
"surname" -> new JTextField(20),
"age" -> new JTextField(3))
setLayout(new BorderLayout)
val formPanel = new JPanel
formPanel setLayout (new GridLayout(formFields.size, 1))
generateForm(formPanel)
getContentPane add formPanel
pack
setVisible(true)
def generateForm(formPanel: JPanel) = {
var leftPanel: JPanel = null
var rightPanel: JPanel = null
var gridPanel: JPanel = null
formFields foreach (entry => {
leftPanel = new JPanel(new FlowLayout)
leftPanel.getLayout().asInstanceOf[FlowLayout] setAlignment FlowLayout.LEFT
rightPanel = new JPanel(new FlowLayout)
rightPanel.getLayout.asInstanceOf[FlowLayout] setAlignment FlowLayout.LEFT
gridPanel = new JPanel(new GridLayout(1, 2))
leftPanel add new JLabel(entry._1 + ':')
rightPanel add entry._2
gridPanel add leftPanel
gridPanel add rightPanel
formPanel add gridPanel
})
}
}
def getFormContent: HashMap[String, String] = {
val formData = HashMap[String, String]()
formFields foreach (entry => {
formData += (entry._1 -> entry._2.getText)
})
return formData
}
def eraseForm : Unit = {
formFields foreach (entry => {
entry._2 setText ""
})
}
private def addListeners = {
}
}
请参阅 ItemController 类中的注释。如果我在注释所在的语句和注释之间留一个空行,newItem setVisible true
那么代码不会出错。
提前致谢。