如何在Scalafx中创建两个场景

发布于 2025-02-06 11:57:17 字数 1534 浏览 1 评论 0原文

我是使用Scalafx创建GUI的新手。我正在尝试使用以下代码创建两个场景,但是会遇到一些错误,

import com.sun.glass.ui.Application
import scalafx.event.ActionEvent
import scalafx.event.EventHandler
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.control.Label
import scalafx.scene.layout.StackPane
import scalafx.scene.layout.VBox
import scalafx.stage.Stage



class example extends Application {
  var scene1: Scene = null
  var scene2: Scene = null
  override def start(primaryStage: Stage): Unit =  { primaryStage.setTitle("My First JavaFX GUI")
    //Scene 1
    val label1 = new Label("This is the first scene")
    val button1 = new Button("Go to scene 2"){
      onAction = (e: ActionEvent) => {
        primaryStage.setScene(scene2)
      }
    }
    val layout1 = new VBox(20)
    layout1.getChildren.addAll(label1, button1)
    scene1 = new Scene(layout1, 300, 250)


    //Scene 2
    val label2 = new Label("This is the second scene")
    val button2 = new Button("Go to scene 1") {
      onAction = (e: ActionEvent) => {
        primaryStage.setScene(scene1)
      }
    }
    val layout2 = new VBox(20)
    layout2.getChildren.addAll(label2, button2)
    scene2 = new Scene(layout2, 300, 250)

    primaryStage.setScene(scene1)
    primaryStage.show()
  }
}

The error is:
 found   : scalafx.event.ActionEvent => Unit
[error]  required: javafx.event.EventHandler[javafx.event.ActionEvent]
[error]       onAction = (e: ActionEvent) => {}

如何声明在场景之间切换的操作事件?

如果有人能有所帮助,这真的很有帮助

I am new to creating GUI using scalafx. I am trying to create two scenes with the following code but getting some error

import com.sun.glass.ui.Application
import scalafx.event.ActionEvent
import scalafx.event.EventHandler
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.control.Label
import scalafx.scene.layout.StackPane
import scalafx.scene.layout.VBox
import scalafx.stage.Stage



class example extends Application {
  var scene1: Scene = null
  var scene2: Scene = null
  override def start(primaryStage: Stage): Unit =  { primaryStage.setTitle("My First JavaFX GUI")
    //Scene 1
    val label1 = new Label("This is the first scene")
    val button1 = new Button("Go to scene 2"){
      onAction = (e: ActionEvent) => {
        primaryStage.setScene(scene2)
      }
    }
    val layout1 = new VBox(20)
    layout1.getChildren.addAll(label1, button1)
    scene1 = new Scene(layout1, 300, 250)


    //Scene 2
    val label2 = new Label("This is the second scene")
    val button2 = new Button("Go to scene 1") {
      onAction = (e: ActionEvent) => {
        primaryStage.setScene(scene1)
      }
    }
    val layout2 = new VBox(20)
    layout2.getChildren.addAll(label2, button2)
    scene2 = new Scene(layout2, 300, 250)

    primaryStage.setScene(scene1)
    primaryStage.show()
  }
}

The error is:
 found   : scalafx.event.ActionEvent => Unit
[error]  required: javafx.event.EventHandler[javafx.event.ActionEvent]
[error]       onAction = (e: ActionEvent) => {}

How can I declare the action events to switch between the scenes?

It would be really helpful if anyone can help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-02-13 11:57:17

在ScaleFX中,您的第一个故障排除是检查您是否包括ScaleFX隐式转换魔法:

import scalafx.Includes._

在您的情况下,这将解决动作处理程序的问题。

此外,您不应使用com.sun beackafx中的软件包和javafx代码中均不使用。在Scalafx中,您应该改用JFXAPP3。这是您的代码更正以使用JFXAPP3进行一些较小的更正:

import scalafx.Includes._
import scalafx.application.JFXApp3
import scalafx.application.JFXApp3.PrimaryStage
import scalafx.event.{ActionEvent, EventHandler}
import scalafx.scene.Scene
import scalafx.scene.control.{Button, Label}
import scalafx.scene.layout.VBox
import scalafx.stage.Stage

object CreteTwoScenes1App extends JFXApp3 {
  var scene1: Scene = _
  var scene2: Scene = _

  override def start(): Unit = {

    stage = new PrimaryStage {
      title = "My First JavaFX GUI"
    }

    // Scene 1
    val label1 = new Label("This is the first scene")
    val button1 = new Button("Go to scene 2") {
      onAction = (e: ActionEvent) => {
        stage.setScene(scene2)
      }
    }
    val layout1 = new VBox(20)
    layout1.children ++= Seq(label1, button1)

    scene1 = new Scene(layout1, 300, 250)

    // Scene 2
    val label2 = new Label("This is the second scene")
    val button2 = new Button("Go to scene 1") {
      onAction = (e: ActionEvent) => {
        stage.setScene(scene1)
      }
    }
    val layout2 = new VBox(20)
    layout2.children ++= Seq(label2, button2)
    scene2 = new Scene(layout2, 300, 250)

    stage.setScene(scene1)
  }
}

您还可以以更惯用的Scalafx构建器样式进行重写。使用Scala 3也可以使其变得更加紧凑(无需卷发):

import scalafx.Includes.*
import scalafx.application.JFXApp3
import scalafx.scene.Scene
import scalafx.scene.control.{Button, Label}
import scalafx.scene.layout.VBox

object CreteTwoScenes2App extends JFXApp3:

  override def start(): Unit =
    lazy val scene1: Scene = new Scene(300, 200):
      root = new VBox(20):
        children = Seq(
          new Label("This is the first scene"),
          new Button("Go to scene 2"):
            onAction = () => stage.setScene(scene2)
        )

    lazy val scene2: Scene = new Scene(300, 200):
      root = new VBox(20):
        children = Seq(
          new Label("This is the second scene"),
          new Button("Go to scene 1"):
            onAction = () => stage.setScene(scene2)
        )

    stage = new JFXApp3.PrimaryStage:
      title = "My First JavaFX GUI"
      scene = scene1

In ScaleFX, your first troubleshooting is to check if you included the ScaleFX implicit conversion magic:

import scalafx.Includes._

In your case, that will fix the issue with the action handler.

Additionally, you should not use com.sun packages neither in ScalaFX nor in JavaFX code. In ScalaFX you should use JFXApp3 instead. Here is your code corrected to use JFXApp3 with some minor corrections:

import scalafx.Includes._
import scalafx.application.JFXApp3
import scalafx.application.JFXApp3.PrimaryStage
import scalafx.event.{ActionEvent, EventHandler}
import scalafx.scene.Scene
import scalafx.scene.control.{Button, Label}
import scalafx.scene.layout.VBox
import scalafx.stage.Stage

object CreteTwoScenes1App extends JFXApp3 {
  var scene1: Scene = _
  var scene2: Scene = _

  override def start(): Unit = {

    stage = new PrimaryStage {
      title = "My First JavaFX GUI"
    }

    // Scene 1
    val label1 = new Label("This is the first scene")
    val button1 = new Button("Go to scene 2") {
      onAction = (e: ActionEvent) => {
        stage.setScene(scene2)
      }
    }
    val layout1 = new VBox(20)
    layout1.children ++= Seq(label1, button1)

    scene1 = new Scene(layout1, 300, 250)

    // Scene 2
    val label2 = new Label("This is the second scene")
    val button2 = new Button("Go to scene 1") {
      onAction = (e: ActionEvent) => {
        stage.setScene(scene1)
      }
    }
    val layout2 = new VBox(20)
    layout2.children ++= Seq(label2, button2)
    scene2 = new Scene(layout2, 300, 250)

    stage.setScene(scene1)
  }
}

You can also rewrite that in more idiomatic ScalaFX builder style. Using Scala 3 can make it much more compact too (no curly braces needed):

import scalafx.Includes.*
import scalafx.application.JFXApp3
import scalafx.scene.Scene
import scalafx.scene.control.{Button, Label}
import scalafx.scene.layout.VBox

object CreteTwoScenes2App extends JFXApp3:

  override def start(): Unit =
    lazy val scene1: Scene = new Scene(300, 200):
      root = new VBox(20):
        children = Seq(
          new Label("This is the first scene"),
          new Button("Go to scene 2"):
            onAction = () => stage.setScene(scene2)
        )

    lazy val scene2: Scene = new Scene(300, 200):
      root = new VBox(20):
        children = Seq(
          new Label("This is the second scene"),
          new Button("Go to scene 1"):
            onAction = () => stage.setScene(scene2)
        )

    stage = new JFXApp3.PrimaryStage:
      title = "My First JavaFX GUI"
      scene = scene1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文