管理 main 函数
我创建了一个类 A
,它读取文本文件并更改文本中的一些行,代码写在该类的 main()
中。我还创建了另一个具有 Frame
的类 B
,该 Frame
包含目录和按钮的文本文件列表。我不知道该怎么做是这样的:当我单击列表中的所选项目并单击按钮时,将调用函数 main 并由 main()
读取所选项目。
欢迎任何建议并提前致谢。
I created a class A
that reads a text file and alters some lines within the text, and the code is written in main()
of this class. I also created another class B
that has a Frame
, the Frame
contains a list of text files of a directory and a button. What I don't know how to do is this: When I click on the selected item on the list and click the button, the function main is called and the selected item is being read by main()
.
Any suggestion is welcomed and thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当静态 main 方法中有代码时,您就有了一个非面向对象的代码块,该代码块不会轻易被程序中其他面向对象的兼容部分使用。这里最好的解决方案是从 main 中获取除最少代码之外的所有代码,并创建一个真正的 OOP 兼容类,该类可以更容易地被其他类使用。
您的另一个问题是文本处理代码与 GUI 代码的混合,这可能需要进一步修复,特别是如果文本处理代码需要一段时间才能完成或者占用 CPU 资源。如果是这样,您需要注意在 GUI 线程的后台线程中进行文本处理,而且,如果文本处理代码位于行为良好的 OOP 类中,则执行起来会容易得多。
When you have code in the static main method, you have a non-object oriented code block, one that won't easily be used by other object oriented-compliant portions of your program. The best solution here is to get all code but the minimal out of main and create a true OOP-compliant class, one that can be more easily used by your other classes.
Your other issue is the mixing of your text-processing code with your GUI code, and this may require further fixing, especially if the text-processing code takes a while to complete or is a CPU hog. If so, you'll need to take care to do the text-processing in a thread that is background to the GUI thread, and again, this is much more easily performed if the text-processing code is in a well-behaved OOP class.
main 方法就像任何其他方法一样,您可以以相同的方式调用它。
您可能想要使用后台线程,而不是在处理时锁定 GUI。 ;)
The main method is just like any other method, you call it the same way.
You might want to use a background thread instead of locking up the GUI while it is processing. ;)