简体   繁体   English

Scala中的JavaFX找不到启动方法

[英]JavaFX in Scala can't find launch method

Trying to see if I can make and run a JavaFX program in Scala I've run into a curious problem, the launch method can't be found... 试图查看是否可以在Scala中制作并运行JavaFX程序我遇到了一个奇怪的问题,找不到启动方法...

Here's the Java code: 这是Java代码:

package example;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.*;

public class Program extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Test.fxml"));

        Scene scene = new Scene(root, 300, 275);

        stage.setTitle("FXML Test");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

And the direct-to-scala translation: 以及直接标量转换:

package example

import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene._
import javafx.stage._

object Program extends Application {
  override def start(stage: Stage): Unit = {
    val root = FXMLLoader.load(getClass getResource "Test.fxml")

    val scene = new Scene(root, 300, 275)

    stage setTitle "FXML Test Scala"
    stage setScene scene
    stage.show
  }

  def main(args: Array[String]): Unit = launch(args) // this bit fails
}

I chose to make a 1:1 translation to see if it'd even work to begin with, but like I said before, the compiler doesn't know what launch is in the Scala one... 我选择进行1:1转换,看看它是否甚至可以开始工作,但是就像我之前说过的那样,编译器不知道Scala版本中的launch是什么...

So what am I doing wrong here? 那我在做什么错呢? And more importantly, how do I resolve this? 更重要的是,我该如何解决?

Asked over on the Google+ Scala group and got the following working solution thanks to Witold Czaplewski : 感谢Witold Czaplewski ,在Google+ Scala小组中被问到并获得以下工作解决方案:

package example

import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene._
import javafx.stage._

object Program {
  def main(args: Array[String]): Unit = 
    Application.launch(classOf[Program], args: _*)
}

class Program extends Application {
    override def start(stage: Stage): Unit = {
    val root = FXMLLoader.load(getClass() getResource "Test.fxml")
    val scene = new Scene(root, 300, 275)
    stage setTitle "FXML Test Scala"
    stage setScene scene
    stage.show
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM