是否有一些构建Scala + JavaFX桌面应用程序的指南或演练?
我很难找到一个好的源码,我正在使用IntelliJ IDEA作为集成开发环境。
即使是最简单的桌面hello world示例也会有很大帮助,因为我几乎不知道从哪里开始。
更新:这是我现在所拥有的:
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import javafx.scene.control.Label
class Test extends Application {
override def start(primaryStage: Stage) {
primaryStage.setTitle("Sup!")
val root = new StackPane
root.getChildren.add(new Label("Hello world!"))
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
}
object Test {
def main(args: Array[String]) {
val t = new Test
t.start(new Stage)
}
}
运行它,我得到了:
线程" main“java.lang.IllegalStateException中出现
异常:不在FX应用程序线程上;currentThread =main
如何让它显示带有标签的hello world窗口?
发布于 2012-08-26 21:39:17
在编写基于Scala的JavaFX应用程序时,有几件事需要知道。
首先,这是一个示例hello world应用程序:
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import javafx.scene.control.Label
class Test extends Application {
println("Test()")
override def start(primaryStage: Stage) {
primaryStage.setTitle("Sup!")
val root = new StackPane
root.getChildren.add(new Label("Hello world!"))
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
}
object Test {
def main(args: Array[String]) {
Application.launch(classOf[Test], args: _*)
}
}
运行它,你应该会得到:
下面是一个用Java语言编写的官方hello world示例:http://docs.oracle.com/javafx/2/get_started/hello_world.htm
主要区别是:
启动实际application.
def main()
必须指定它将在def main()
如果你只是尝试用Application.launch(args : _*)
直接运行应用程序,你会得到这个错误:
线程"main“中出现
异常java.lang.RuntimeException:错误: class Test$不是javafx.application.Application的子类
要了解有关JavaFX的更多信息,只需阅读官方文档:http://docs.oracle.com/javafx/index.html
发布于 2017-09-16 04:29:48
您可以使用这种方式。
class BuildFx extends Application{
override def start(primaryStage: Stage): Unit = {
primaryStage.setTitle("Scala")
var btn=new Button("Say Hello Scala")
val root=new StackPane()
root.getChildren().add(btn)
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.show()
}
def launchIt():Unit={
Application.launch()
}
}
///////////////////////////////////////////////////////////
object Init{
def main(args: Array[String]): Unit = {
val buildFx=new BuildFx
buildFx.launchIt()
}
}
发布于 2016-08-11 23:54:06
我能够在scala_swing中更令人满意地解决这个问题,因为你可以用参数实例化一个实例,然后在它上面调用main来启动Swing。
此解决方案允许在FX应用程序中以使用静态变量和可能的其他问题为代价来获取参数。其一,这肯定不是多线程安全的。
package hack
/**
* Created by WorkDay on 8/11/16.<br>
* <br>
* HelloTest shows a method which allows parameters to be passed
* into your javaFX application as it is started
* this allows it to be connected to non-FX code that existed before it.
*
* You could also pass a reference to the Application back
* into the non-FX code if needed.
*/
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.layout.StackPane
import javafx.stage.Stage
import javafx.scene.control.Label
case class Data(data: String)
object SomeOtherCode extends App {
HelloTest.launch(Data("brave"), Data("new"))
}
object HelloTest {
var data1: Data = _
var data2: Data = _
def launch(data1: Data, data2: Data) = {
HelloTest.data1 = data1
HelloTest.data2 = data2
Application.launch(classOf[HelloTest])
}
}
private class HelloTest extends Application {
val data1: Data = HelloTest.data1
val data2: Data = HelloTest.data2
override def start(primaryStage: Stage) {
primaryStage.setTitle("Sup!")
val root = new StackPane
root.getChildren.add(new Label(s"Hello ${data1.data} ${data2.data} world!"))
primaryStage.setScene(new Scene(root, 300, 300))
primaryStage.setX(0)
primaryStage.setY(0)
primaryStage.show()
}
}
https://stackoverflow.com/questions/12124657
复制相似问题