在scala控制台中打开和关闭JavaFx应用程序 [英] Open and close JavaFx apps in scala console

查看:209
本文介绍了在scala控制台中打开和关闭JavaFx应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个示例:

/*
 * Copyright 2013 ScalaFX Project
 * All right reserved.
 */
package scalafx.ensemble.example.charts

import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.collections.ObservableBuffer
import scalafx.scene.chart.LineChart
import scalafx.scene.chart.NumberAxis
import scalafx.scene.chart.XYChart

/** A chart in which lines connect a series of data points. Useful for viewing
  * data trends over time.
  *
  * @see scalafx.scene.chart.LineChart
  * @see scalafx.scene.chart.Chart
  * @see scalafx.scene.chart.Axis
  * @see scalafx.scene.chart.NumberAxis
  * @related charts/AreaChart
  * @related charts/ScatterChart
  */
object BasicLineChart extends JFXApp {

  stage = new JFXApp.PrimaryStage {
    title = "Line Chart Example"
    scene = new Scene {
      root = {

        val xAxis = NumberAxis("Values for X-Axis", 0, 3, 1)
        val yAxis = NumberAxis("Values for Y-Axis", 0, 3, 1)

        // Helper function to convert a tuple to `XYChart.Data`
        val toChartData = (xy: (Double, Double)) => XYChart.Data[Number, Number](xy._1, xy._2)

        val series1 = new XYChart.Series[Number, Number] {
          name = "Series 1"
          data = Seq(
            (0.0, 1.0),
            (1.2, 1.4),
            (2.2, 1.9),
            (2.7, 2.3),
            (2.9, 0.5)).map(toChartData)
        }

        val series2 = new XYChart.Series[Number, Number] {
          name = "Series 2"
          data = Seq(
            (0.0, 1.6),
            (0.8, 0.4),
            (1.4, 2.9),
            (2.1, 1.3),
            (2.6, 0.9)).map(toChartData)
        }

        new LineChart[Number, Number](xAxis, yAxis, ObservableBuffer(series1, series2))
      }
    }
  }
}

object Main {
  BasicLineChart.main(Array(""))
}

我发送的行 BasicLineChart.main(Array())到控制台,一个JavaFx窗口在其中打开折线图,控制台被阻止。当我关闭图表窗口时,我恢复了对scala控制台的访问。当我再次尝试启动同一个窗口时,出现错误:

What I send the line BasicLineChart.main(Array("")) to the console, a JavaFx window shows up with a line chart in it, and the console is blocked. When I close the chart window, I recover access to scala console. When I try to fire up the same window again, I get an error:

scala>   BasicLineChart.main(Array(""))
java.lang.IllegalStateException: Application launch must not be called more than once
  at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:162)
  at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:143)
  at javafx.application.Application.launch(Application.java:191)
  at scalafx.application.JFXApp$class.main(JFXApp.scala:242)
  at BasicLineChart$.main(<console>:23)
  ... 35 elided

所以我有两个问题:


  1. 如何在控制台中启动JavaFx应用程序而不阻止?

  1. How to launch a JavaFx app in the console without blocking?

如何避免上述错误?






更新1



根据freenode的一些建议,我将BasicLineChart更改为类并执行此操作:


Update 1

Following some advice from freenode, I changed the BasicLineChart into a class and did this:

object Main {
  val x = new BasicLineChart()
  x.main(Array(""))
  val y = new BasicLineChart()
  y.main(Array(""))
}

仍有同样的错误。

推荐答案

问题2,快速查看 JFXApp 它调用javafx.application.Application.launch,docs 这里。该页面描述了生命周期,表明只能调用一次启动。基本上JFXApp希望成为整个应用程序的入口点,因此不应多次调用。

On question 2, from a quick look at JFXApp it calls through to javafx.application.Application.launch, docs here. That page describes the life cycle, indicating that launch must only be called once. Basically JFXApp expects to be the entry point for a whole application, so shouldn't be called multiple times.

如果您希望能够快速重新启动应用程序,我会考虑使用run或runMain而不是使用控制台从SBT运行它。

If you want to be able to quickly relaunch your app, I would consider just running it from SBT using run or runMain rather than using the console.

问题1,如果您决定从SBT运行,您应该能够分叉在运行中, SBT docs ,特别是尝试在运行中添加 fork:= true build.sbt

On question 1, if you do decide to run from SBT you should be able to fork in run, there are details in the SBT docs, specifically try adding fork in run := true to build.sbt.

这篇关于在scala控制台中打开和关闭JavaFx应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆