簡體   English   中英

ScalaFX TableView-簡單的示例編譯器錯誤

[英]ScalaFX TableView - simple example compiler error

我只是想熟悉ScalaFX,而我的Scala知識也仍處於萌芽階段。 在以下簡單問題中,您能幫我什么忙嗎?

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.{TableCell, TableColumn, TableView}

object Gui extends JFXApp {

case class listInfo(text: String, subtexts: List[String], thumbnail: String, attributeField: Int)

  val demo = ObservableBuffer[listInfo](
    new listInfo("dewdfasc", subtexts = List("first", "second"), "tdsam", 1),
    new listInfo("hgfhfghn", subtexts = List("first", "second"), "tdsacdsm", 2)
  )

  stage = new PrimaryStage {
    title = "ScalaFX Hello World"
    scene = new Scene {
      root = TableView[listInfo](demo) {
        columns ++= List(
          new TableColumn[listInfo, String] {
            text = "band"
            cellValueFactory = {_.value.text}
            prefWidth = 100
          },
          new TableColumn[listInfo, String] {
            text = "thumbnail"
            cellValueFactory = {_.value.thumbnail}
            prefWidth = 100
          }
        )
      }
    }
  }

}

具有以下編譯器錯誤:

Error:(20, 23) object TableView does not take type parameters.
      root = TableView[listInfo](demo) {
Error:(21, 9) not found: value columns
        columns ++= List(
Error:(24, 41) type mismatch;
 found   : String
 required: scalafx.beans.value.ObservableValue[String,String]
            cellValueFactory = {_.value.text}
Error:(29, 41) type mismatch;
 found   : String
 required: scalafx.beans.value.ObservableValue[String,String]
            cellValueFactory = {_.value.thumbnail}

我的環境:

Java 1.8

斯卡拉2.12

scalafx 2.11-8.0.144-R12

我應該糾正什么以便編譯?

提前致謝!

import scalafx.application.JFXApp
import scalafx.beans.property.StringProperty
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.{ TableColumn, TableView }

object Gui extends JFXApp {
    case class ListInfo(_text: String, _subtexts: List[String], _thumbnail: String, _attributeField: Int) {
        val text = new StringProperty(this, "text", _text)
        val thumbnail = new StringProperty(this, "thumbnail", _thumbnail)
    }

    val demo = ObservableBuffer[ListInfo](
        ListInfo("dewdfasc", List("first", "second"), "tdsam", 1),
        ListInfo("hgfhfghn", List("first", "second"), "tdsacdsm", 2)
    )

    stage = new JFXApp.PrimaryStage {
        title.value = "ScalaFX Hello World"
        scene = new Scene {
            root = new TableView[ListInfo](demo) {
                columns ++= List(
                    new TableColumn[ListInfo, String] {
                        text = "band"
                         cellValueFactory = _.value.text
                         prefWidth = 100
                    },
                    new TableColumn[ListInfo, String] {
                        text = "thumbnail"
                        cellValueFactory = _.value.thumbnail
                        prefWidth = 100
                    }
                )
            }
        }
    }
}
  • 使用大寫類型(例如ListInfo)
  • 創建即時案例類時,新鍵盤是多余的
  • cellValueFactory期望使用scalafx.beans.value.ObservableValue [String,String]而不是字符串(請參閱案例類ListInfo val)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM