1

我遇到了一些我认为是 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那么代码不会出错。

提前致谢。

4

1 回答 1

5
newItem eraseForm
newItem setVisible true

被解析为

newItem.eraseForm(
newItem).setVisible(true);

但你想要的是:

newItem.eraseForm;
newItem.setVisible(true);

省略点和括号意味着 Scala 编译器应用“运算符表示法”。这意味着您的表达式始终需要采用obj meth param. 有可能,obj meth;但正如您所见,此表单需要用分号停止。您可以自己插入它,也可以让编译器通过添加一个空行、一个以下一行中的关键字开头的表达式或右大括号和括号来推断它。

从 2.10 开始,如果您编写,您也会收到警告,obj meth因为这些后缀运算符可能是有害的,正如您在问题中看到的那样。

有关“运算符符号”规则的更多信息,请参阅这两个问题及其答案:

于 2013-08-23T19:53:47.287 回答