如何从控制台主方法运行 SimpleSwingApplication?
我用 Scala 编写了第一个控制台应用程序,并用 Scala 编写了第一个 Swing 应用程序 - 对于后者,入口点是扩展 SimpleSwingApplication 的对象中的 top 方法>。
不过,我仍然想通过 main 方法,并从那里调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有类似的情况:
您只需调用
MySimpleApp.main
即可从控制台启动它。当您混合SimpleSwingApplication
特征时,会添加一个main
方法。查看 scaladoc。If you have something like:
You can just call
MySimpleApp.main
to start it from the console. Amain
method is added when you mixSimpleSwingApplication
trait. Have a look at the scaladoc.下面是最基本的示例:
从命令行执行 scala MainApplication.scala 来启动 Swing 应用程序。
Here is the most basic example:
Execute
scala MainApplication.scala
from the command line to start the Swing application.如果您重写从 SimpleSwingApplication 继承的 main 方法,您可以做任何您想做的事情:
If you override the main method that you inherit from SimpleSwingApplication, you can do whatever you want:
我需要 main.args 才能在 SimpleSwingApplication 中使用。我想给出一个文件名作为 CLI 中的参数进行处理,或者然后使用 JFileChooser以防命令行参数列表为空。
我不知道是否有简单的方法在 SimpleSwingApplication 中使用命令行参数,但我如何让它工作是在 demoapp.class 中定义:
然后通过以下方式启动应用程序
演示[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:
但我想我更喜欢前一种方法,与分离的主对象。它比后一种方法至少少一级缩进。
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:
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:
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.