如何从控制台主方法运行 SimpleSwingApplication?

发布于 2024-12-11 23:21:16 字数 329 浏览 0 评论 0原文

我用 Scala 编写了第一个控制台应用程序,并用 Scala 编写了第一个 Swing 应用程序 - 对于后者,入口点是扩展 SimpleSwingApplication 的对象中的 top 方法>。

不过,我仍然想通过 ma​​in 方法,并从那里调用 top - 或执行其他等效操作(例如创建一个窗口并“运行”它)。

怎么做呢?

如果您好奇为什么,GUI 是可选的,所以我想解析命令行参数,然后决定显示(或不显示)应用程序窗口。

I wrote my first console app in Scala, and I wrote my first Swing app in Scala -- in case of the latter, the entry point is top method in my object extending SimpleSwingApplication.

However I would like to still go through main method, and from there call top -- or perform other equivalent actions (like creating a window and "running" it).

How to do it?

Just in case if you are curious why, the GUI is optional, so I would like to parse the command line arguments and then decide to show (or not) the app window.

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

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

发布评论

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

评论(4

与他有关 2024-12-18 23:21:16

如果您有类似的情况:

object MySimpleApp extends SimpleSwingApplication {
  // ...
}

您只需调用MySimpleApp.main即可从控制台启动它。当您混合 SimpleSwingApplication 特征时,会添加一个 main 方法。查看 scaladoc

If you have something like:

object MySimpleApp extends SimpleSwingApplication {
  // ...
}

You can just call MySimpleApp.main to start it from the console. A main method is added when you mix SimpleSwingApplication trait. Have a look at the scaladoc.

绿萝 2024-12-18 23:21:16

下面是最基本的示例:

import swing._

object MainApplication {
  def main(args: Array[String]) = {
    GUI.main(args)
 }

  object GUI extends SimpleSwingApplication {
    def top = new MainFrame {
      title = "Hello, World!"
    }
  }
}

从命令行执行 scala MainApplication.scala 来启动 Swing 应用程序。

Here is the most basic example:

import swing._

object MainApplication {
  def main(args: Array[String]) = {
    GUI.main(args)
 }

  object GUI extends SimpleSwingApplication {
    def top = new MainFrame {
      title = "Hello, World!"
    }
  }
}

Execute scala MainApplication.scala from the command line to start the Swing application.

木有鱼丸 2024-12-18 23:21:16

如果您重写从 SimpleSwingApplication 继承的 main 方法,您可以做任何您想做的事情:

object ApplicationWithOptionalGUI extends SimpleSwingApplication {

  override def main(args: Array[String]) =
    if (parseCommandLine(args))
      super.main(args) // Starts GUI
    else
      runWithoutGUI(args)

...

}

If you override the main method that you inherit from SimpleSwingApplication, you can do whatever you want:

object ApplicationWithOptionalGUI extends SimpleSwingApplication {

  override def main(args: Array[String]) =
    if (parseCommandLine(args))
      super.main(args) // Starts GUI
    else
      runWithoutGUI(args)

...

}
寄与心 2024-12-18 23:21:16

我需要 main.args 才能在 SimpleSwingApplication 中使用。我想给出一个文件名作为 CLI 中的参数进行处理,或者然后使用 JFileChooser以防命令行参数列表为空。

我不知道是否有简单的方法在 SimpleSwingApplication 中使用命令行参数,但我如何让它工作是在 demoapp.class 中定义:

class demoSSA(args: Array[String]) extends SimpleSwingApplication {
    ....
    var filename: String = null
    if (args.length > 0) {
        filename = args(0)
    } else {
        // use JFileChooser to select filename to be processed 
        // https://stackoverflow.com/questions/23627894/scala-class-running-a-file-chooser
    }
    ....
}

object demo {
  def main(args: Array[String]): Unit = {
     val demo = new demoSSA(args)
     demo.startup(args)
  }
}

然后通过以下方式启动应用程序
演示[args],通过将文件名作为 CLI 参数提供或将其留空,程序使用 JFileChooser 来询问它。

有没有办法获取 SimpleSwingApplication 单例对象中 main() 的参数?
因为在重写的 SimpleSwingApplication.main 中解析“文件名”并在单例对象中使用“文件名”不起作用,所以需要声明它(val args:Array [String] = null;),而不仅仅是定义(val args :数组[字符串];)。并且从 SSA 继承的单例对象不能有参数。

(编辑)
找到了另一种方式:
如果整个demoSSA放在重写的main()或startup()中,那么top MainFrame需要在外部定义为top = null并从startup()重新声明它为this.top:

object demo extends SimpleSwingApplication {  
  var top: MainFrame = null
  override def startup(args: Array[String]) {
    ...  // can use args here
    this.top = new MainFrame {
      ... // can use args here also
    };
    val t = this.top
    if (t.size == new Dimension(0,0)) t.pack()
      t.visible = true
  }
}

但我想我更喜欢前一种方法,与分离的主对象。它比后一种方法至少少一级缩进。

I needed main.args to be usable in SimpleSwingApplication. I wanted to give a filename to process as an argument in CLI, or then use JFileChooser in case the command line argument list was empty.

I do not know if there is simple method to use command line args in SimpleSwingApplication, but how I got it working was to define, in demoapp.class:

class demoSSA(args: Array[String]) extends SimpleSwingApplication {
    ....
    var filename: String = null
    if (args.length > 0) {
        filename = args(0)
    } else {
        // use JFileChooser to select filename to be processed 
        // https://stackoverflow.com/questions/23627894/scala-class-running-a-file-chooser
    }
    ....
}

object demo {
  def main(args: Array[String]): Unit = {
     val demo = new demoSSA(args)
     demo.startup(args)
  }
}

Then start the app by
demo [args], by giving filename as a CLI argument or leaving it empty and program uses JFileChooser to ask it.

Is there a way to get to the args of main() in SimpleSwingApplication singleton object?
Because it doesn't work to parse 'filename' in overridden SimpleSwingApplication.main and to use 'filename' in the singleton object it needs to be declared (val args: Array[String] = null;), not just defined (val args: Array[String];). And singleton objecs inherited from SSA cannot have parameters.

(Edit)
Found one another way:
If whole demoSSA is put inside overridden main() or startup(), then top MainFrame needs to defined outside as top = null and re-declare it from startup() as this.top:

object demo extends SimpleSwingApplication {  
  var top: MainFrame = null
  override def startup(args: Array[String]) {
    ...  // can use args here
    this.top = new MainFrame {
      ... // can use args here also
    };
    val t = this.top
    if (t.size == new Dimension(0,0)) t.pack()
      t.visible = true
  }
}

But I think I like the former method more, with separated main object.It has at least one level indentation less than the latter method.

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